Note that there are some explanatory texts on larger screens.

plurals
  1. POKnockoutJS subscribe to property changes with Mapping Plugin
    primarykey
    data
    text
    <p>Is there anyway I can tell the knockout mapping plugin to subscribe to all property changes call a certain function?</p> <p>I realize I can manually subscribe to the property change event in this manner:</p> <pre><code>var viewModel = { name: ko.observable('foo'), } // subscribe manually here viewModel.name.subscribe(function(newValue){ // do work }) </code></pre> <p>I would like to be able to generically subscribe though, since my view models may vary, I don't want to hardcode the property names. I created a function that does this, but it may not be the best approach. It works over all browsers except IE7 and below.</p> <p>Here I take a viewmodel as an argument and try to reflect on it subscribing to the properties:</p> <pre><code>function subscribeToKO(data) { $.each(data, function (property, value) { if (getType(value) == "Object") data[property] = subscribeToKO(value); else if (getType(value) == "Array") { $.each(value, function (index, item) { item = subscribeToKO(item); }); } else { if (value.subscribe) { value.subscribe(function (newValue) { // do work }); } } }); return data; } </code></pre> <p>Like I said this works, but since I am using the mapping pluging I was hoping there was a hook I could use to provide it with a function that will generically subscribe to property changes.</p> <p>Something like:</p> <pre><code>mapping = { create: function(options){ options.data.subscribe(function(newValue){ // do work ??? }); } } ko.mapping.fromJS(viewModel, mapping); </code></pre> <p>Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload