How can I "refuse" a knockout?

I have a view model:

var myViewModel = function (){
    var self= this;
    self.someObservable = ko.observable();

    var someObservableSubscription = self.someObservable.subscribe(function(){
        //some stuff including a different ajax 'Get'
    });

    self.ajaxPost = function(){
        //some ajax
        //on done call this method
        cleanModel();
    }

    var cleanModel = function(){
        someObservableSubscription.dispose();
        self.someObservable('');

        //now i want to resubscribe to my function here
        //this is where I need help

}

      

Should I do another one again self.someObservable.subscribe(function(){ //some function})

? I want to clear the value in a variable without losing the observable, but if I clear it while it has a subscription it tries to make this ajax call null.

My guess is that another way to fix this would be to wrap my subscription functions in if

that checks that the observable value is valid before doing anything. Which one would be the best / are there any other ways to do this?

+3


source to share


1 answer


Instead of calling dispose

and then " undispose

" <--- (this code is composed), the correct way to handle the changing value is to wrap the internals of the subscribe function in if

.

See example below:



var someObservableSubscription = self.someObservable.subscribe(function(){
    if (self.someObservable() > 0) {
        //some stuff including a different ajax 'Get'
    }
});

      

0


source







All Articles