Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <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>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. COIt feels a bit of a hack having to basically cache the data in an observable array. The best solution (without doing async bindings, which to remind people the original question was) is to just attach a property at runtime to the object to store the picture, so there is no need for the ajax binding. Also just to be clear in my scenario the async bit is the localstorage lookup, there is no server side component or ajax requests to simplify the use case.
      singulars
    2. COThe thing that concerns me, and it's a code 'smell' thing, is when I see someone do something like: `<span data-bind="text: $root.GetFormattedString($data)"></span>`. It sounds like an event handler needs to be setting an observable value somewhere or a computed should be used for the binding. I have a couple options that might help that I'll update my answer with.
      singulars
    3. COThing is an observable is meant to be a 2 way binding between the viewmodel and the ui. So it is something you want to be updated when it changes in the UI, granted this is not always the case but is in most cases. However in this example this is not something that is updated, it is set once when the template is rendered and then forgotten about, so it seems pointless going through the overhead of setting up an observable just to basically cache a readonly value. There is no business logic applied to this so its not like its obfuscating business concerns, so I dont see it as a worry.
      singulars
 

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