2

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.

2
  • push() is for arrays, it adds a new items to the array. Commented Nov 20, 2013 at 9:15
  • @Sébastien sorry,i corrected it Commented Nov 20, 2013 at 9:22

2 Answers 2

1

All you have to do is set the observable

success : function(responseData) {
    var data = ko.toJS(responseData);
    AppViewModel.agent(data.agent);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can just assign new data to array:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.