所以,在我使用淘汰赛的第一周,我想我已经有了一个很好的原型: (修剪并删除ajaxy调用)
http://jsfiddle.net/NelsonLamprecht/39dfx/
"use strict";
var steeringTeamSheetViewModel = function (serviceUrl) {
var self = this;
self.viewModel = ko.mapping.fromJS([]);
self.InitializeAjax = function () {
//abbreviated
};
self.GetData = function () {
//abbreviated
var data = {
"Sections": [{
"ProjectType": "BUSINESS EXPANSION",
"Projects": [{
"ID": "767a46a2-ddba-435c-a9f9-fdb9f0175337",
"ItemOrder": 0,
"ProjectName": "project abc"
}, {
"ID": "0e36d7da-92e6-4f1b-939d-936d6e759115",
"ItemOrder": 0,
"ProjectName": "project abc"
}, {
"ID": "f6e447d4-955d-48e0-bcdf-6db9044b7a89",
"ItemOrder": 0,
"ProjectName": "project a"
}]
}, {
"ProjectType": "OPER & MAINT - EFFICIENCY",
"Projects": [{
"ID": "9883a3c8-d01e-4fc9-8f66-9b46d720afde",
"ItemOrder": 0,
"ProjectName": "project q"
}]
}, {
"ProjectType": "OTHER",
"Projects": [{
"ID": "f1ccfa79-c5b1-4880-b5a1-1c2350e709e1",
"ItemOrder": 0,
"ProjectName": "project 1"
}]
}]
};
self.ProcessRetrievedData(data);
};
self.ProcessRetrievedData = function (retrievedData) {
self.viewModel(retrievedData);
};
self.GetData();
};
var steeringTeamSheetService = 'someurl';
var sts = new steeringTeamSheetViewModel(steeringTeamSheetService);
ko.applyBindings(sts);
我现在要做的是将ko.oberservableArray更改为另一个绑定,例如 .indexed()一个漂浮在那里。
http://jsfiddle.net/NelsonLamprecht/39dfx/19/
"use strict";
var steeringTeamSheetViewModel = function (serviceUrl) {
var self = this;
var mapping = {
'sections': {
create: function(options) {
alert(options);
}
}
}
self.viewModel = ko.mapping.fromJS([],mapping);
self.InitializeAjax = function () {
//abbreviated
};
self.GetData = function () {
//abbreviated
var data = {
"Sections": [{
"ProjectType": "BUSINESS EXPANSION",
"Projects": [{
"ID": "767a46a2-ddba-435c-a9f9-fdb9f0175337",
"ItemOrder": 0,
"ProjectName": "project abc"
}, {
"ID": "0e36d7da-92e6-4f1b-939d-936d6e759115",
"ItemOrder": 0,
"ProjectName": "project abc"
}, {
"ID": "f6e447d4-955d-48e0-bcdf-6db9044b7a89",
"ItemOrder": 0,
"ProjectName": "project a"
}]
}, {
"ProjectType": "OPER & MAINT - EFFICIENCY",
"Projects": [{
"ID": "9883a3c8-d01e-4fc9-8f66-9b46d720afde",
"ItemOrder": 0,
"ProjectName": "project q"
}]
}, {
"ProjectType": "OTHER",
"Projects": [{
"ID": "f1ccfa79-c5b1-4880-b5a1-1c2350e709e1",
"ItemOrder": 0,
"ProjectName": "project 1"
}]
}]
};
self.ProcessRetrievedData(data);
};
self.ProcessRetrievedData = function (retrievedData) {
self.viewModel(retrievedData);
//I think I should be using something like
//self.viewModel = ko.mapping.fromJSON(retrievedData,mapping,{});
};
self.GetData();
};
var steeringTeamSheetService = 'someurl';
var sts = new steeringTeamSheetViewModel(steeringTeamSheetService);
ko.applyBindings(sts);
但是,如果没有这一切,我认为我在设置模型和从Web服务(c#)获取数据时遵循正确的模式。
有人可以验证我这样做是否正确以及如何帮助创建模式?
答案 0 :(得分:0)
据我了解您的问题,您尝试通过ajax下载数据并将其存储在steeringTeamSheetViewModel的属性“viewModel”中。你的方式对我来说没问题。
您不应该覆盖self.viewModel。因为如果ko.applyBindings已经运行到那一点,你将破坏绑定。
self.ProcessRetrievedData = function (retrievedData) {
// map the data from the viewModel and update self.viewModel
self.viewModel(ko.mapping.fromJSON(retrievedData,mapping,{}));
};
请记住,如果您拥有大型视图模型,ko.mapping将变得非常慢。