javascript - Knockout - added observable not updating on new objects -


for ui purposes, when load array viewmodel based on add new property each object based on other properties:

    item.foreach(function (party) {         if (party.acknowledgementdate() === null) {             party.agreed = ko.observable(false);         }         else {             party.agreed = ko.observable(true);         }         vm.parties.push(party);     }); 

"parties" defined ko.observablearray when page starts.

the items in array edited in separate ui window. when changes saved , window closed, call function update values:

function updateagreed() {     vm.parties().foreach(function (i) {         if (i.acknowledgementdate() === null) {             i.agreed(false);         }         else {             i.agreed(true);         }     }); } 

this works fine, , makes me happy. problem arrives when users create new party item. we're using breeze too, go off data service requests entity framework create new object of appropriate type, add observable:

 var lp = manager.createentity('party_dto'. { [an array of initial values] });  lp.agreed = ko.observable('');  return lp; 

thanks breeze, adds parties observablearray because it's related same parent object. can call updateagreed again populate agreed observable appropriate value.

logically, work expected - can step through , watch agreed observable of new item added , populated expected values. problem comes in ui - doesn't update having changed. yet running same code against already-loaded object cause ui update.

i'm stumped this. can't replicate in fiddle because create objects in breeze , not on fly - , making mock version without breeze works perfectly. why observables update on loaded objects, same observable not update on new object?

there few things see need addressed. one, since using breeze, take advantage of model constructors , initializers. wherever defining properties models, add following code -

metadatastore.registerentitytypector(     'party', null, partyinitializer);  function partyinitializer(party) {     party.agreed = ko.observable(false); } 

now of party entities have agreed property can access. next, make sure aren't setting party's parent navigation property in createentity method, break binding.

var lp = manager.createentity('party'. { [an array of initial values] }); lp.parentparty(something); // set parent here return lp; 

this make sure before party bound parent , shown in view, of properties set. when set navigation property, show in view happy-like.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

java.util.scanner - How to read and add only numbers to array from a text file -

iphone - Three second countdown in cocos2d -