2

現在直面している問題があります。オブジェクトのリストを含む監視可能な配列があります。配列のオブジェクトのプロパティを更新するたびに、ブラウザに反映されません。replace、removeなどのすべてのノックアウト関数を使用しています。更新は監視可能な配列で行われますが、ブラウザでは行われません。

これが私の問題のサンプルです:

 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
4

1 に答える 1

3

あなたは言う:

配列のオブジェクトのプロパティを更新するたびに、ブラウザに反映されません。

ko.observableUI を自動的に更新するには、監視可能な配列内のオブジェクトのプロパティも設定する必要があります。

例えば:

var anObservableArray = ko.observableArray([    
  { name: "A", type: "Type A" }    
]);

// some later point
anObservableArray()[0].name = "B";

観測可能ではないため、UI を更新しません。name

でも、

var anObservableArray = ko.observableArray([    
  { name: ko.observable("A"), type: ko.observable("Type A") }    
]);

// some later point
anObservableArray()[0].name("B");

.. UI を更新して、名前 B をname観察可能なものとして表示します。

編集:(コードが質問に追加された後)

したがって、コードから次のことがわかります。

 answer=GetAnswerFromViewModel(parentcourseItemID); 
 answer.CommentCount--;                     
 answer.CommentList.splice(CommentIndex,1); 
 answer.CommentText=''; 

観察可能なプロパティで回答を返すと仮定GetAnswerFromViewModelすると、次のように書く必要があります。

 answer=GetAnswerFromViewModel(parentcourseItemID); 
 answer.CommentCount(answer.CommentCount()--);                     
 answer.CommentList.splice(CommentIndex,1); 
 answer.CommentText(''); 

回答のプロパティが観察可能でない場合、UI は更新されません。

于 2012-05-30T10:31:33.443 に答える