开发者

stopListening to Mojo.Event

开发者 https://www.devze.com 2023-02-24 11:02 出处:网络
I have several event listeners in a webOS app and each one I set up like this: this.controller.listen(

I have several event listeners in a webOS app and each one I set up like this:

this.controller.listen(
    'a开发者_开发知识库Widget', 
    Mojo.Event.widgetEvent, 
    this.respondToWidgetEvent.bindAsEventListener(this)
);

And to stop listening I write code like this:

this.controller.stopListening(
    'aWidget', 
    Mojo.Event.widgetEvent, 
    this.respondToWidgetEvent.bindAsEventListener(this)
);

However, I realize now that my listeners may not be stopping. When I call bindAsEventListener on a function, do I get back the same object each time? If not, does stopListening make sure to remove the appropriate listener anyway?


If I remember correctly each call to bindAsEventListener() returns a new instance. Prevent that action by calling it once and setting a var:

bindToWidget = this.respondToWidgetEvent.bindAsEventListener(this);

this.controller.listen(
    'aWidget', 
    Mojo.Event.widgetEvent, 
    bindToWidget
);

this.controller.stopListening(
    'aWidget', 
    Mojo.Event.widgetEvent, 
    bindToWidget
);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号