Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>"is there any way to get knockout to somehow become callback aware?"</code></p> <p>Yes, you can use subscriptions. All observables, observableArrays, and computeds inherit from the type subscribable so you can do this:</p> <pre><code>var foo = ko.observable("foo"); foo.subscribe(function (newValue) { // When foo updates, this function is called }); </code></pre> <p>With subscriptions you can even set up temporary subscriptions and cancel them later if you no longer need them.</p> <pre><code>var fooSub = foo.subscribe(function (newValue) { // When foo updates, this function is called }); // Somewhere else in the app... fooSub.dispose(); </code></pre> <p>By default, subscriptions subscribe to a topic called 'change'. This means when the value of an observable changes, it calls any subscribers with the newValue (thus the name of the parameter), but you can also set up a subscription that subscribes to the topic 'beforeChange' in order to perform logic before some value changes.</p> <pre><code>foo.subscribe(function (oldValue) { // Do logic on the oldValue here }, null, 'beforeChange'); </code></pre> <p>And you can read about this in knockout's <a href="http://knockoutjs.com/documentation/observables.html" rel="nofollow">documentation</a>. But you can also subscribe to custom topics if you want. By default, when values of observables change, the 'beforeChange' and 'change' topics fire off before and after the value changes (respectively). But you can subscribe to a custom topic that you can later manually trigger, to notify any subscribers listening in on that topic.</p> <pre><code>foo.subscribe(function (value) { // Do logic when observable notifies subscribers to the 'customTopic' topic }, null, 'customTopic'); // Somewhere else in the app... var value = "bar"; foo(value); foo.notifySubscribers(value, 'customTopic'); </code></pre> <p>In this way you can set up communication between separate view models that have no direct references to each other. This is my cursory understanding of how to do this, you can learn more about it by watching Ryan Niemeyer's tips and tricks <a href="http://www.knockmeout.net/2013/09/devlink-2013-ko-tips.html" rel="nofollow">video</a>. Specifically the subscriptions section.</p> <p>In this way you can perform a kind of callback in knockout. Also check out Ryan's <a href="https://github.com/rniemeyer/knockout-postbox/" rel="nofollow">Knockout-postbox</a> library that extends observables to subscribeTo and publishOn these topics.</p> <hr> <p>You can also look into jQuery <a href="http://api.jquery.com/jQuery.Deferred/" rel="nofollow">$.Deferreds</a> which is the underlying piece that $.ajax requests use. It's not a knockout callback but it is a kind of callback.</p> <p>Let me know if that is more of what you're looking for.</p>
 

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