所以,在我使用淘汰赛的第一周,我想我得到了一个很好的原型:(修剪并删除了 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#) 获取数据时没有遵循正确的模式。
有人可以验证我这样做是对的以及对创建模式的帮助吗?