Mojo.Event.stopListening

From WebOS101

Jump to: navigation, search

Good programming practice dictates that an application should stop listening to Widgets when it is no longer necessary to do so, such as when a scene is no longer active, it is not necessary to listen to button taps. In order to use Mojo.Event.stopListening() effectively, here are some things to consider.

  • The handler function passed to Mojo.Event.stopListening() must exactly match the handler function which was used with Mojo.Event.listen(). Simply calling the function will not do. Save a reference to the bound function in an object.
  • If the call to Mojo.Event.listen() used true as a fourth argument, then Mojo.Event.stopListening() must also include true in the arguments.
  • Most of the time, the location of Mojo.Event.listen() should dictate where Mojo.Event.stopListening() is called. In the following example, Mojo.Event.listen() is in activate() and Mojo.Event.stopListening() is in deactivate(). For listeners created in setup(), use Mojo.Event.stopListening() in cleanup().
Example
SomeAssistant.prototype.setup = function() {
this.widgetAttributes = {
// the widget's attributes
};
this.widgetModel = {
// the widget's model
};
this.widgetActionHandler = this.widgetAction.bindAsEventListener(this); // create a re-usable object by calling the real function and bind it to 'this'
 
this.controller.setupWidget('widget', this.widgetAttributes, this.widgetModel); // set up the widget
};
 
SomeAssistant.prototype.activate = function() {
// Mojo.Event.tap is just an example, use any event that applies
Mojo.Event.listen(this.controller.get('widget'), Mojo.Event.tap, this.widgetActionHandler);
};
 
SomeAssistant.prototype.deactivate = function() {
// use the same objects to stop listening on the widget
Mojo.Event.stopListening(this.controller.get('widget'), Mojo.Event.tap, this.widgetActionHandler);
};
 
SomeAssistant.prototype.widgetAction = function() {
// do some stuff
};
Personal tools