简体   繁体   English

如何获取嵌套视图模型的实例

[英]How to get instance of nested view model- knockout

The view has a heading followed by section with has submenus. 该视图具有一个标题,后跟具有子菜单的部分。 the design for the viewmodels is below: 视图模型的设计如下:

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);  

                        });

        });

I need to call the AddSections method of the nested SettingsViewModel from MainViewModel instance. 我需要从MainViewModel实例调用嵌套的SettingsViewModel的AddSections方法。 How to achieve this? 如何实现呢?

Thanks in advance. 提前致谢。

Your problem is that parameter index from sections loop hides index from products loop. 你的问题是,参数index从部分循环隐藏index从产品循环。 Just use another parameter name: 只需使用另一个参数名称:

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);
        });
    });
};

Fiddle 小提琴

I would use a EventAggregator to decouple viewmodels, I've written this lightweight EventAggregator 我将使用EventAggregator解耦视图模型,我已经编写了这个轻量级的EventAggregator

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

Subscribe: 订阅:

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

Publish: 发布:

MyApp.eventAggregator.publish(new MyApp.DeleteCustomerMessage(this));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM