Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsubscribe in Observable KnockOutJS

Im currently using KnockOut JS and i conducted some research about when the observable is notified it will fire a function which is like this

function FunctionToSubscribe()
{

}

var TestObservable = ko.observableArray([]);

TestObservable.subscribe(FunctionToSubscribe);

i am subscribing FunctionToSubscribe in this event

im currently thinking is there a way to unsubscribe it? like we do in c#? when unsubscribing events anyone have an idea regarding this???

like image 924
Don Avatar asked Nov 27 '14 06:11

Don


2 Answers

The subscribe function returns the "subscription" object which has a dispose method what you can use to unsubscribe:

var TestObservable = ko.observableArray([]);

var subscription = TestObservable.subscribe(FunctionToSubscribe);

//call dispose when you want to unsubscribe
subscription.dispose(); 

See also in the documentation: Explicitly subscribing to observables

like image 199
nemesv Avatar answered Nov 14 '22 21:11

nemesv


You can use dispose method.

function FunctionToSubscribe()
{

}

var TestObservable = ko.observableArray([]);

// subscribe
var subscriber = TestObservable.subscribe(FunctionToSubscribe);

// unsubscribe
subscriber.dispose();
like image 43
sadrzadehsina Avatar answered Nov 14 '22 20:11

sadrzadehsina