Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h3>The basic solution</h3> <p>You asked how one can unsubscribe something subscribed to KnockoutJS. </p> <p>Knockout observable subscriptions expose a <a href="http://knockoutjs.com/documentation/observables.html" rel="nofollow"><code>.dispose()</code></a> function just for that.</p> <p>When you subscribe:</p> <pre><code>var subscription = toDate.subscribe(function (newValue) { //do nothing }); </code></pre> <p>Clean up:</p> <pre><code>subscription.dispose(); </code></pre> <p>If you want this for multiple subscriptions, you might want to consider pushing them into an array and then cleaning up after all of them, something like</p> <pre><code>var subs = []; function subSafe(observable,function){ subs.push(observable.subscribe(function)); } function unsubscribeAll(){ subs.forEach(function(subscription){ subscription.dispose(); }); } </code></pre> <hr> <h3>A self contained example.</h3> <p>Let's say we have two buttons and a value, one button increments that value. We have a listener to the value changing which has an <code>alert</code>. When we click the second button, the subscription is removed.</p> <p><strong>HTML:</strong></p> <pre><code>&lt;button data-bind="click: $root.inc"&gt;Inc&lt;/button&gt; &lt;button data-bind="click: $root.unsub"&gt;Remove subscription&lt;/button&gt; </code></pre> <p><strong>JavaScript:</strong></p> <pre><code>var vm = { val:ko.observable(0), inc:function(){ vm.val(vm.val()+1); }, }; vm.sub = vm.val.subscribe(function(newVal){ alert("Increased to "+newVal); }); vm.unsub = function(){ vm.sub.dispose(); } ko.applyBindings(vm); </code></pre> <h3><a href="http://jsfiddle.net/n9MLY/" rel="nofollow">Here is a short fiddle illustrating the process</a></h3>
    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.
    3. VO
      singulars
      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