2017-11-20 7 views
0

서버에서 데이터를 가져온 후 내 ViewModel을 업데이트하기 위해 ko.mapping.fromJS을 사용하고 있습니다. 그러나 그것은 나를위한 문제입니다.ko.mapping.fromJS 다시 만들지 않고 개체를 변경하십시오.

ko.mapping.fromJS 이후 업데이트 된 속성으로보기가 표시됩니다. 그러나 매번 속성은 새 데이터로 다시 생성되지만 기존 속성은 업데이트되지 않습니다.

새 데이터로 속성을 다시 생성하는 대신 ViewModel의 업데이트 된 속성을 얻으려면 어떻게해야합니까?

mapping plugin documentation에서 설명한 바와 같이

답변

0

Let’s say you have a JavaScript object that looks like this: 

var data = { 
    name: 'Scot', 
    children: [ 
     { id : 1, name : 'Alicw' } 
    ] 
} 
You can map this to a view model without any problems: 

var viewModel = ko.mapping.fromJS(data); 
Now, let’s say the data is updated to be without any typos: 

var data = { 
    name: 'Scott', 
    children: [ 
     { id : 1, name : 'Alice' } 
    ] 
} 
Two things have happened here: name was changed from Scot to Scott and children[0].name was changed from Alicw to the typo-free Alice. You can update viewModel based on this new data: 

ko.mapping.fromJS(data, viewModel); 

마지막 문자열은 새 데이터로 뷰 모델을 업데이트하는 방법을 보여줍니다.