Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In knockout... you'll have to do something like that:</p> <pre><code>function Xxx(val) { var self = this; this.x = ko.observable(val); this.change = function() { // "that" may be different to "self" in some // cases... var that = this; $.ajax({ url: '...', type: 'get', success: function(data) { self.x(5); }, error: function(a) { console.log('got an error'); } }); }; } var newX = new Xxx(1); newX.change(); ko.computed(function () { // This will get called everytime // newX.x is changed console.log(newX.x()); }); </code></pre> <p>When you create a variable, that may change, you have to create it as an observable. The observable is in fact a function that you call. When called, it will update its inner value and it will also trigger any changes wherever the observable is "observed"</p> <p>In no way you should try to do <code>this.x = 5</code>. It will override the actual observable object and thus it will never trigger every observer of a change. </p> <h1>edit</h1> <p>In case you're interested to understand how does computed works. A computed variable is a function that will <code>listen</code> to observables. When the <code>computed</code> is created, it will be called once to check which observables where called from within it. It's a way to "track" dependencies. In this example, you should see at least two console log. one with 1, and then with 5. </p> <p>In my case, the computed variable is kind of anonymous since it isn't affected anywhere. Also in some case, you may need to observe one variable but use multiple observables. To prevent update on any other used observables. There are some ways to do that. You can either return after you "watched" the observables you needed.</p> <p>Or you can create a sub function that will be triggered a little after the computed with <code>setTimeout(..., 0);</code>. There are a couple of ways to achieve some really nice tricks. </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. 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.
 

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