Knockout: Prevenet a property from adding into a computed's dependency list

189 views
Skip to first unread message

habib.st...@gmail.com

unread,
May 1, 2015, 12:29:47 PM5/1/15
to knock...@googlegroups.com

I have an infobox which is either open or closed depending on the value if isVisible property.

self.infoboxState = ko.computed(function () {
     if (!ko.computedContext.isInitial()) {
          self.performAdditionalBehaviors();
     }
     return ko.unwrap(self.isVisible)? 'infobox-open' : 'infobox-closed';
}, this);

When we set the value of isVisible for the second time it would also execute performAdditionalBehaviors() method. This method contains an observable property isDim and the infoboxState becomes dependent on both observables which means any change in the isDimproperty will also now call the infoboxState. I verified that from getDependenciesCount() which is now set to 2.

Issue is that I don't want infoboxState to get called whenever isDim is changed. Is there any way to tell the computed to not add a property into it's dependencies list?

Thanks.

Michael Best

unread,
May 1, 2015, 6:15:27 PM5/1/15
to knock...@googlegroups.com, habib.st...@gmail.com
In Knockout 3.3.0, you can use ko.ignoreDependencies:

self.infoboxState = ko.computed(function () {
     
if (!ko.computedContext.isInitial()) {
         ko.ignoreDependencies(self.performAdditionalBehaviors, self);
     }
     return ko.unwrap(self.isVisible)? 'infobox-open' : 'infobox-closed';
}, this);
Reply all
Reply to author
Forward
0 new messages