There is issue which I am facing right now.I have observable array which contains list of objects.Whenever I updating property of any object of array.It is not reflected on browser.I have use all knockout functions like replace,remove.But the updation coming in observable array but not in browser.
Here is a sample of my issue:
var ViewModel=new {
self=this;
self.List=ko.observableArray([]);
}
$(function(){
ko.applyBinding(ViewModel);
})
$.post('/url',{},function(data){
ViewModel.List(data); //data is list having 4 property having CommentList as again object-->id,title,description,CommentList--->commenttitle,commentdescription
})
//During change of property of commentList
$.post('/updateComment',{},function(obj){//Here obj-->obj.Commenttitle="some title",obj.commentdescription='some description'
//Let say there require update 4th object of List and 2nd property of CommentList
ViewModel.AnswerList()[4].CommentList.splice(2,1,obj);
})
//But nothing updation on browser
You say:
Whenever I updating property of any object of array it is not
reflected on browser.
The properties on the objects in the observable array also need to be set to ko.observable
for your UI to be automatically updated.
For example:
var anObservableArray = ko.observableArray([
{ name: "A", type: "Type A" }
]);
// some later point
anObservableArray()[0].name = "B";
would NOT update your UI as name
is not an observable.
However,
var anObservableArray = ko.observableArray([
{ name: ko.observable("A"), type: ko.observable("Type A") }
]);
// some later point
anObservableArray()[0].name("B");
..would update your UI to display the name B as name
is an observable.
EDIT: (After code was added to question)
So from your code you have:
answer=GetAnswerFromViewModel(parentcourseItemID);
answer.CommentCount--;
answer.CommentList.splice(CommentIndex,1);
answer.CommentText='';
assuming GetAnswerFromViewModel
returns an answer with observable properties, you should be writing:
answer=GetAnswerFromViewModel(parentcourseItemID);
answer.CommentCount(answer.CommentCount()--);
answer.CommentList.splice(CommentIndex,1);
answer.CommentText('');
If the properties on your answer are not observables then your UI will not update.