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
);
精彩评论