0

ビューには見出しがあり、その後にサブメニューのあるセクションが続きます。ビューモデルのデザインは次のとおりです。

SettingsViewModel = function (pName) {
    var self = this;
    self.productName = ko.observable(pName), //heading
    self.sections = ko.observableArray([
            { checkboxID: ko.observable(), checkboxIDState: ko.observable(), sectionname: ko.observable(), sectionState: ko.observable() } 
        ]),  //submenus

    self.Addsections = function (checkboxid, sIdState, sName, sState) {
        this.sections.push({ checkboxID: checkboxid, checkboxIDState: sIdState, sectionname: sName, sectionState: sState });
    }
};


function MainViewModel() {
    var self = this;
    self.products = ko.observableArray([]);
    self.AddProducts= function (pname) {
        self.products.push(new SettingsViewModel(pname));
    }
};


$(document).ready(function () {
        VM = new MainViewModel();
        ko.applyBindings(VM, document.getElementById("divd"));
    data= []; //some dummy data
    CallMEnus(data);

 });

 function CallMEnus(data) {
        var str = "";

        $(data).each(function (index, products) {
            VM.AddProducts(products.name);

                        $(products.section).each(function (index, section) {

                            var ChkboxId = "data";  
                            var chkboxIdState = 'datt';
                            var chkboxIdState += " checked";
                        }
            //how to call the products add section method?
                            VM.products()[index].Addsections(ChkboxId, chkboxIdState, section.name, section.state);  

                        });

        });

ネストされた SettingsViewModel の AddSections メソッドを MainViewModel インスタンスから呼び出す必要があります。これを達成する方法は?

前もって感謝します。

4

2 に答える 2

1

あなたの問題は、セクションループのパラメーターが製品ループからindex隠れていることです。index別のパラメータ名を使用してください:

function CallMEnus(data) {
    var str = "";

    $(data).each(function (index, products) {
        VM.AddProducts(products.name);

        $(products.section).each(function(i, section) { // here
            var id = "data";
            var state = "checked";
            VM.products()[index].Addsections(id, state, section.name, section.state);
        });
    });
};

フィドル

于 2013-06-10T11:39:53.947 に答える
-1

ビューモデルを切り離すために EventAggregator を使用します。この軽量の EventAggregator を作成しました。

http://jsfiddle.net/wJtun/4/

申し込む:

MyApp.eventAggregator.subscribe(MyApp.DeleteCustomerMessage, this.customerDeleted, this);

公開:

MyApp.eventAggregator.publish(new MyApp.DeleteCustomerMessage(this));
于 2013-06-10T11:03:34.587 に答える