replace observableArray with new data

╄→гoц情女王★ 提交于 2019-12-14 02:17:23

问题


I have a viewmodel like

   AppViewModel = {
    agent : ko.observableArray([ {
        name : 'test',
        age  : '23'             
    }])         
};

My json data comes like

{"agent":[{"name":"john","age":"23"},{"name":"conor","age":"23"}]}

for ajaxcall evry 3 sec

How to replace the view model with new data

I tried

success : function(responseData) {
    var data = ko.toJS(responseData);
    //AppViewModel.agent.push(data);
     AppViewModel.agent.replace(agent,data);
}

but doest work.


回答1:


All you have to do is set the observable

success : function(responseData) {
    var data = ko.toJS(responseData);
    AppViewModel.agent(data.agent);
}



回答2:


You can just assign new data to array:

success : function(responseData) {
    var data = ko.toJS(responseData);
    AppViewModel.agent(data);
}


来源:https://stackoverflow.com/questions/20091586/replace-observablearray-with-new-data

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