1
使用ko.mapping.fromJSON只要定義了嵌套對象,我就可以從JSON進行綁定。當我將嵌套對象更新爲空或空時,綁定就會中斷。Knockout映射嵌套對象綁定在null時不更新
在此代碼中,我有五個不同的數據集,我使用model.nextDataset函數進行迭代。當數據集到達第四個和第五個變體時,它將中斷與內部對象的綁定。
var datasets = ["{\"id\": 1,\"reference\": \"reference1\",\"inner\": {\"id\": 1,\"name\": \"name1\"}}",
"{\"id\": 1,\"reference\": \"reference2\",\"inner\": {\"id\": 1,\"name\": \"name2\"}}",
"{\"id\": 1,\"reference\": \"reference3\",\"inner\": {\"id\": null,\"name\": null}}",
"{\"id\": 1,\"reference\": \"reference3\",\"inner\": {}}",
"{\"id\": 1,\"reference\": \"reference3\",\"inner\": null}"];
var mapping = {
'create': function (options) {
return new Model(options.data);
},
'key': function (data) {
return ko.utils.unwrapObservable(data.id);
},
'inner': {
'create': function (options) {
return new InnerModel(options.data);
}
},
'key': function (data) {
return ko.utils.unwrapObservable(data.id);
}
};
/*
var innerMapping = {
'key': function(data) {
return ko.utils.unwrapObservable(data.id);
}
};
*/
var InnerModel = function (data) {
var self = this;
ko.mapping.fromJS(data, {}, self);
self.innerName = ko.computed(function() {
return self.name() + " computed";
}, self);
};
var Model = function (data) {
var self = this;
ko.mapping.fromJS(data, mapping, self);
self.innerName = ko.computed(function() {
return self.inner.name() + " computed";
}, self);
}
var model = {
dataset: ko.observable(0),
model: ko.mapping.fromJSON(datasets[0], mapping, model),
nextDataset: function() {
model.dataset(model.dataset() + 1);
if (datasets.length === model.dataset()) {
model.dataset(0);
}
ko.mapping.fromJSON(datasets[model.dataset()], mapping, model.model);
}
}
ko.applyBindings(model);
伴隨HTML:
<div id="testko">
<p>Dataset: <span data-bind="text: dataset"></span><p>
<input type="text" data-bind="value: model.reference" /><br>
<div data-bind="with: model.inner"><input type="text" data-bind="value: innerName" /><br></div>
<input type="text" data-bind="value: model.innerName" /><br>
<input type="button" data-bind="click: nextDataset" value="Next Dataset" />
</div>
我已經做了的jsfiddle,試圖證明這一點:
我相信,我錯誤地以某種方式映射,但我避風港」 t已經足夠長的時間來使用淘汰賽來說明原因。
您正在訪問inner.name,但inner爲空! – LostInComputer