1

所以,在我使用淘汰赛的第一周,我想我得到了一个很好的原型:(修剪并删除了 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#) 获取数据时没有遵循正确的模式。

有人可以验证我这样做是对的以及对创建模式的帮助吗?

4

1 回答 1

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 将变得非常慢。

于 2013-01-23T13:18:41.553 回答