我使用过搜索引擎,并找到了很多答案,但我遇到了问题。问题是子集合映射似乎没有触发。在下面的代码中,警告"组已创建",但显示了" Root创建的" -alert。
var mapping = {
'Groups': {
create: function (options) {
var lang = ko.observable(ko.mapping.fromJS(options.data));
alert('Groups created');
return lang;
}
},
create: function (options) {
var innerModel = ko.mapping.fromJS(options.data);
alert('Root created');
innerModel.ajaxSave = submitFormWithAjax;
return innerModel;
}
}
var viewModel = null;
function loadViewModel(model, offlineCache, key) {
viewModel = ko.mapping.fromJS(model, mapping);
if (typeof extendViewModel == 'function') {
extendViewModel();
}
ko.applyBindings(viewModel);
}
我的json数据看起来像这样
{
"Groups": [
{
"Texts": [
{
"Language": {
"Id": 1,
"Name": "English",
"Lcid": 2057
},
"Id": 1,
"Value": "Display name"
},
{
"Language": {
"Id": 2,
"Name": "Swedish",
"Lcid": 1053
},
"Id": 2,
"Value": "Visningsnamn"
}
],
"Id": 1,
"InternalName": "3dc075e3-ff96-4044-b9bb-4a0404912866",
"DisplayName": "Display name"
}
],
"AvailableLanguages": [
{
"Id": 1,
"Name": "English",
"Lcid": 2057
},
{
"Id": 2,
"Name": "Swedish",
"Lcid": 1053
}
]
}
为什么我的群组创建的提醒已被解雇?
答案 0 :(得分:0)
由于某些原因你无法使用映射插件在多个级别上创建函数,你可以这样做
RootViewModel = function(data) {
this.stuffAddedToViewModelBeforeMapping = "Hej på dig";
var mapping = {
Groups: {
create: function(options) {
return new GroupViewModel(options.data);
}
},
AvailableLanguages: {
create: function(options) {
return new LanguageViewModel(options.data);
}
}
};
ko.mapping.fromJS(data, mapping, this);
};
LanguageViewModel = function(data) {
ko.mapping.fromJS(data, {}, this);
}
GroupViewModel = function(data) {
ko.mapping.fromJS(data, {}, this);
};
Lycka直到!