I am applying binding in one of the view model methods which is bound to click event. When i click for the second time, the binding is done again, and view model data fails to show up in the view. Can i have a condition where i can check if the div is already bound to a view model?
Here is My Jsfiddle
var VM;
$(document).ready(function () {
VM = new MainViewModel();
ko.applyBindings(VM,document.getElementById("familyDiv"));
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([]);
};
AdditionalDocument = function () {
var self = this;
self.docName = ko.observable();
self.index = ko.observable();
};
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 = [];
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.showSubMenu2= true;
self.productModel.submenu = ko.observable(SubProductMenus1);
ko.applyBindings(VM.productModel, document.getElementById("subFamilyAccProduct"));
};
self.fetchProductItemsForProduct = function (famName, data, event) {
var SubProductMenus2 = [];
self.subMenuModel = new ProductSubMenu();
self.subMenuModel.submenuName = famName.submenuName;
self.subMenuModel.selectedSubMenuName = famName.submenuName;
self.ShowSubMenu = true;
self.showSubMenu2 = true;
var productItems=[{shortDesc:'subProd1'},{shortDesc: 'subProd2'}, {shortDesc: 'subProd3'}];
$(productItems).each(function (index, value) {
//console.log("short Desc: "+ productItems[index].shortDesc);
var temp2= productItems[index].shortDesc;
var temp = new ProductSubMenu2();
temp.submenu2Name = temp2;
self.subMenuModel.submenu2.push(temp);
SubProductMenus2.push(temp);
});
//console.log("item coount: "+ self.subMenuModel.submenu2.length);
ko.applyBindings(VM.subMenuModel, document.getElementById("ProductItemsForProd"));
};
self.ShowProductInfoBrowse = function (subFamily, item) {
//console.log("sub family name is :" + subFamily + ", "+ item);
var familyname = VM.productModel.selectedProductName();
self.submenu2Model = new ProductSubMenu2();
self.submenu2Model.submenu2Name = item;
self.submenu2Model.selectedSubMenu2Name = item;
additionalD=[];
var temp = new AdditionalDocument();
temp.docName = "PDF document";
additionalD.push(temp);
var temp2 = new AdditionalDocument();
temp2.docName = "test document";
additionalD.push(temp2);
console.log("doc count; "+ additionalD.length);
self.propertyModel = new Properties();
self.propertyModel.pName = ko.observable("Product Testing");
self.propertyModel.shortDesc = ko.observable("short Description");
self.propertyModel.longDesc = ko.observable("long Description");
self.propertyModel.additionalDocs= ko.observable(additionalD);
ko.applyBindings(VM.propertyModel, document.getElementById("ProductDetailsDiv"));
};
}
Thanks!
To determine if a view model has already been bound to a particular node, you can use
ko.dataFor(yourNode)
If the result is truthy, then something has already been bound. Your example is complex, but I would recommend the following things:
I think that using this kind of code:
click:function(data, event){ $('#subFamilyDiv').show(); $('#familyDiv').hide(); $root.GetSubFamilyForProducts(data, event); }
Is not a good practice, you could use specific knockout bindings, for instance, the visible binding
Also, I do not understand why you're doing a binding in the method:
ko.applyBindings(VM.propertyModel, document.getElementById("ProductDetailsDiv"));
Why not binding everything on document ready and just update your VM.propertyModel when you need it?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With