views:

7

answers:

1

Hi,

I am starting WebOS dev and I have a doubt on where should I start and stop my listeners ? I am reading this book but I couldn't find a clear explanation about this. In the sample the author set the listeners in the setup function but I wonder why? isn't a better idea to set them in activate function and stop them in deactivate function as suggested by the template's comments?

In case I am wrong what kind of events should and shouldn't put in setup and activate functions?

When exactly setup, activate, deactivate, cleanup functions are called?

StoryViewAssistant.prototype.setup = function() {
    //HERE, OK?
    this.nextStoryHandler = this.nextStory.bindAsEventListener(this); 
    this.previousStoryHandler = this.previousStory.bindAsEventListener(this); 
    this.controller.listen("nextStory", Mojo.Event.tap, this.nextStoryHandler); 
    this.controller.listen("previousStory", Mojo.Event.tap,this.previousStoryHandler);
    /* add event handlers to listen to events from widgets */

};

StoryViewAssistant.prototype.activate = function(event) {
    //HERE? 
    /* put in event handlers here that should only be in effect when this scene is active. For example, key handlers that are observing the document */
};

StoryViewAssistant.prototype.deactivate = function(event) {
    //HERE? 
    /* remove any event handlers you added in activate and do any other cleanup that should happen before this scene is popped or another scene is pushed on top */
};

StoryViewAssistant.prototype.cleanup = function(event) {
    //HERE, OK?
    this.controller.stopListening("nextStore", Mojo.Event.tap, this.nextStoryHandler);
};

Thanks in advance

+1  A: 

The scene assistant's setup is called when the scene is created, cleanup is called when it's popped off the stack. In setup, the actual HTML content of the controls isn't available, as the template for the scene hasn't been processed yet. A ready method is called if available after that template processing is done, and this is a good spot to do any other HTML DOM changes. activate is called just before the scene becomes active, while deativate is called when either the scene is being popped or another scene is being pushed on top of this one. activate/*deactivate* are also called when the app is minimized to a card or brought back to full screen.

It's generally best to start and stop event listeners on activate/deactivate -- that keeps their alive time to a minimum, and fewer active listeners makes a more responsive system.

Ben Combee