mvvm - Knockout binding using same objects in multiple times in same page -
viewmodel :
var address = new function { var self = this; self.add1 = ko.observable(); self.add2 = ko.observable(); self.city = ko.observable(); self.state = ko.observable(); self.postal = ko.observable(); }, person = new funcion { var self = this; self.firstname = ko.observable(); self.lastname = ko.observable(); self.address = ko.observable(address); }, company = new function { var self = this; self.salesperson = ko.observable(person); self.shippingaddress = ko.observable(address); self.billingaddress = ko.observable(address); };
view:
<div> <h3>shipping address</h3> <span class="">address1: </span><input type="text" data-bind="value:company.shippingaddress.add1"> <span class="">address2: </span><input type="text" data-bind="value:company.shippingaddress.add2"> <span class="">city: </span><input type="text" data-bind="value:company.shippingaddress.city"> <span class="">state: </span><input type="text" data-bind="value:company.shippingaddress.state"> <span class="">postal: </span><input type="text" data-bind="value:company.shippingaddress.postal"> </div> <div> <h3>billing address</h3> <span class="">address1: </span><input type="text" data-bind="value:company.billingaddress.add1"> <span class="">address2: </span><input type="text" data-bind="value:company.billingaddress.add2"> <span class="">city: </span><input type="text" data-bind="value:company.billingaddress.city"> <span class="">state: </span><input type="text" data-bind="value:company.billingaddress.state"> <span class="">postal: </span><input type="text" data-bind="value:company.billingaddress.postal"> </div>
problem :
in view if change address in 1 place it's changing everywhere. need use observable because i'm displaying details in same page.
any please?
this doesn't make sense - binding two-ways reason, why want bind property not share it? if want each individual have different information use different observable bind to...
function address () { var self = this; self.add1 = ko.observable(); self.add2 = ko.observable(); self.city = ko.observable(); self.state = ko.observable(); self.postal = ko.observable(); }, company = new function { var self = this; self.salesperson = ko.observable(person); self.shippingaddress = ko.observable(new address()); self.billingaddress = ko.observable(new address()); };
in situation using anonymous function instead of constructor. constructor evaluated once , bound same object. anonymous function initialized twice separately.
Comments
Post a Comment