Spankied
Spankied

Reputation: 1885

function method to unsubscribe from observable

Cannot read property 'unsubscribe' of undefined

function record(){
    this.event
}
record.prototype = {
    start: function (){
        keyUp = Rx.Observable.fromEvent(document, "keyup");
        this.event = keyUp.subscribe((key) => {console.log(key)});
    },
    stop: function (){
        this.event.unsubscribe();
    }
}

rec = new record();
rec.start();
setTimeout(rec.stop, 2000);

https://jsfiddle.net/yn70k2g9/

Upvotes: 1

Views: 52

Answers (1)

fehrmann
fehrmann

Reputation: 79

In this context this is the window object and not record.

function record(){
    event;
}
record.prototype = {
    start: function (){
        keyUp = Rx.Observable.fromEvent(document, "keyup");
        record.event = keyUp.subscribe((key) => {console.log(key)});
    },
    stop: function (){
        record.event.unsubscribe();
    }
}

rec = new record();
rec.start();
setTimeout(rec.stop, 2000);

https://jsfiddle.net/yn70k2g9/5/

Upvotes: 1

Related Questions