Knockout mapping and foreach data-bind on table with buttons, missing reference to viewmodel?

为君一笑 提交于 2019-12-06 14:19:27

In your self.UpdateExperienceList function, try the following:

self.UpdateExperienceList = function(data) {
    ko.mapping.fromJSON(data, {}, self);
};

This will update the properties on the self object based on the data from the server.

Your initial creation of the view model also looks wrong to me. I would have though you wanted something more like the below:

var myViewModel = ko.mapping.fromJSON(jsonModel, {}, new ViewModel());

I don't think you need to call ko.applyBindings(?????); again. You should define explicitly your view model:

function MyModel() {
    var self = this;

    self.Data = ko.observableArray([{"Name": "Lionel Messi", "Occupation": "Football player"}, {"Name": "Jason Mraz", "Occupation": "Singer"}, {"Name": "Nicolas Cage", "Occupation": "Film Actor"}]);

    self.update = function(data) {
        self.Data(data);
    }
}

$(function(){
    var model = new MyModel();
    ko.applyBindings(model);
});

As you see I define self.update, you just invoke this function and update your data and in case you want to remove a row from table, you just splice that row out of self.Data()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!