Unsubscribe from Observable KnockOutJS

I am currently using KnockOut JS and I have done some research on when a notification will be notified, it will launch a function that looks like this

function FunctionToSubscribe()
{

}

var TestObservable = ko.observableArray([]);

TestObservable.subscribe(FunctionToSubscribe);

I subscribe FunctionToSubscribein this case

im currently thinking is there a way to unsubscribe? how do we do in c #? when events are unsubscribed, who has an idea regarding this ???

+4
source share
2 answers

The function subscribereturns a subscription object, which has a method disposethat you can use to unsubscribe:

var TestObservable = ko.observableArray([]);

var subscription = TestObservable.subscribe(FunctionToSubscribe);

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

. :

+14

dispose.

function FunctionToSubscribe()
{

}

var TestObservable = ko.observableArray([]);

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

// unsubscribe
subscriber.dispose();
+3

All Articles