我有嵌套的ViewModels,其中MainViewModel包含嵌套VM的实例。
相同的代码在这里:http://jsfiddle.net/nayanakalkur/2YCsN/19/
查看代码:
<div id="familyDiv">
<ul data-bind="foreach: AllProductsModel">
<li data-bind="click:function(data, event){ $('#subFamilyDiv').show(); $('#familyDiv').hide(); $root.GetSubFamilyForProducts(data, event); }">
<a data-bind="text: productname">
</a>
</li>
</ul>
</div>
<div id="subFamilyDiv">
<a id="btnBackToFamily" data-bind="click: function(){ $('#subFamilyDiv').addClass('hidePanel'); $('#familyDiv').removeClass('hidePanel'); }">Back</a>
<div id="subFamilyAccProduct">
<div data-bind="foreach: submenu">
<ul data-bind="text: subMenuName">
</ul>
</div>
</div>
</div>
VM代码:
var VM;
$(document).ready(function () {
VM = new MainViewModel();
ko.applyBindings(VM,document.getElementById("familyDiv"));
ko.applyBindings(VM.productModel, document.getElementById("subFamilyAccProduct"));
FetchProductFamiliesForProductsKO();
});
function FetchProductFamiliesForProductsKO() {
var data=[{family: 'family1'},{family:'family2'},{family:'family3'}];
for (var i = 0; i < data.length; i = i + 1) {
var fam = data[i].family;
console.log(fam);
VM.AddProducts(fam, null, fam);
}
};
ProductMenu= function(name, subProductsMenu1, selectedMenu) {
var self= this;
self.productname = ko.observable(name);
self.submenu = ko.observableArray([]);
self.selectedProductName = ko.observable();
};
ProductSubMenu=function() {
var self = this;
self.submenuName = ko.observable();
self.submenu2 = ko.observableArray([]);
self.selectedSubMenuName = ko.observable();
};
ProductSubMenu2= function() {
var self = this;
self.submenu2Name = ko.observable();
self.properties = ko.observableArray([]);
self.selectedSubMenu2Name = ko.observable();
};
Properties= function() {
var self = this;
self.pName = ko.observable();
self.shortDesc = ko.observable();
self.longDesc = ko.observable();
self.additionalDocs = ko.observableArray([]);
};
function MainViewModel() {
var self= this;
self.productModel = new ProductMenu();
self.subMenuModel = new ProductSubMenu();
self.submenu2Model = new ProductSubMenu2();
self.propertyModel = new Properties();
self.AllProductsModel = ko.observableArray([]);
//if true- show family products, hide sub family products
self.ReturnToFamilyProduct = ko.observable(true);
self.ShowSubMenu = ko.observable(false);
self.showSubMenu2 = ko.observable(false);
self.ShowBackBtnOnSubMenuClick = ko.observable(false);
self.IfDocumentsPresent = ko.observable(true);
//to add product to products array
self.AddProducts = function (name, subProductsMenu1, selectedMenu) {
this.AllProductsModel.push(new ProductMenu(name, subProductsMenu1, selectedMenu));
};
self.GetSubFamilyForProducts = function (data, event) {
this.ShowSubMenu = ko.observable(true);
SubProductMenus1 = [];
//this.ShowBackBtnOnSubMenuClick = ko.observable(true);
var currentElement = data;
$('#subFamilyDiv').show();
$('#btnBackToFamily').html("back: "+currentElement.productname());
self.productModel = new ProductMenu();
self.productModel.productname = ko.observable(currentElement.productname());
self.selectedProductName = ko.observable(currentElement.productname());
var data=[{subFamily: 'subFamily1'},{subFamily:'subFamily2'},{subFamily:'subFamily3'}];
$(data).each(function (index, subFamily) {
var tt= data[index].subFamily;
var temp = new ProductSubMenu();
console.log("subfamily:"+ tt);
temp.submenuName = tt;
SubProductMenus1.push(temp);
});
self.productModel.submenu = ko.observable(SubProductMenus1);
ko.utils.arrayForEach(self.AllProductsModel(), function (product) {
if (product.productname() == currentElement.productname()) {
console.log("found"+ product.submenu().length);
product.submenu = ko.observable(SubProductMenus1);
console.log("found"+ product.submenu().length);
}
})
};
}
在productModel下定义的子菜单无法绑定。
任何帮助都将不胜感激。
谢谢!
答案 0 :(得分:0)
子菜单属性不包含任何项目。在这个fiddle中我只添加了一个假项目,而resuting视图显示了一个fakeSubMenu元素。所以我认为你只在子菜单属性中添加项目,它会起作用。
这就是我所做的:
ProductMenu= function(name, subProductsMenu1, selectedMenu) {
var self= this;
self.productname = ko.observable(name);
self.submenu = ko.observableArray([{subMenuName: 'fakeSubMenuName'}]);
self.selectedProductName = ko.observable();
};
我希望它有所帮助。