null이 아닌 실제 채워진 개체로 전환되는 중첩 된 속성이있는 복잡한 개체가 있습니다. 처음에는 ko.mapping.fromJSON을 통해 모든 것을 잘 맵핑 할 수 있습니다. 이벤트 속성이 null이 될 경우 발생하는 후속 매핑을 유발하고 다른 값을 얻을 때 매핑 미친 간다 :녹아웃 ko.mapping.fromJSON이 업데이트되지 않습니다.
"업데이트!"할 때 버튼을 클릭하면 경고 ('1')가 발생하고 "Test2"가 표시됩니다. 그러나 "업데이트!"를 클릭하십시오. 다시 경고하고 ('2') 화재가 발생하지만 텍스트는 변경되지 않습니다. 어떤 아이디어?
HTML :
<p> <span>Name:</span>
<span data-bind="text: IntroData ? IntroData.Name : TempData.Name"></span>
<button id="update" data-bind="click: Update">Update!</button>
</p>
자바 스크립트 : 당신은 물건과 stuff3에 IntroData 및 TempData에 대해 서로 다른 모양을 제공 한
var ViewModel = function (data) {
var me = this;
ko.mapping.fromJS(data, {}, me);
me.Update = function() {
if (me.Index() == '0' || me.Index() == '2') {
alert('1');
ko.mapping.fromJS(stuff2, {}, me);
} else {
alert('2');
ko.mapping.fromJS(stuff3, {}, me);
}
};
return me;
};
var stuff = {
Index: '0',
IntroData: {
Name: 'Test'
},
TempData: null
};
var stuff2 = {
Index: '1',
IntroData: {
Name: 'Test2'
},
TempData: {
Name: 'Temp2'
}
};
var stuff3 = {
Index: '2',
IntroData: null,
TempData: {
Name: 'Temp3'
}
};
window.viewModel = ko.mapping.fromJS(new ViewModel(stuff));
ko.applyBindings(window.viewModel);