Application of knockout expanders after display

I had a problem adding a knockout extender to the observables after they were created. In the following example, the expander starts on foo every time the value changes as expected, but only once during the first call to bar .

 var viewModel = function(){ var self = this; self.foo = ko.observable(1).extend({ numeric: 1 }); self.bar = ko.observable(1); self.bar.extend({ numeric: 1 }); }; 

Essentially, I am mapping a large JSON object and would like to add extenders after the mapping has occurred with some of the properties. Is there an easy way to do this?

Below is jsfiddle showing the problem:

http://jsfiddle.net/LgxTn/

+8
javascript
source share
1 answer

The problem is that the expander creates a new observable (based on its observed expansion). And this new observable is not used in the second extend call.

To fix this (without changing the extender code), you can assign the response from the extend call to the same resource that he named .

In other words, add self.bar = in front of your last line to have this:

 self.bar = self.bar.extend({ numeric: 1 }); 
+8
source share

All Articles