在Mojo框架(WebOS)中启动和停止监听器

时间:2010-10-23 13:22:05

标签: prototype webos palm-pre mojo palm-pre2

我正在启动WebOS dev,我怀疑我应该从哪里开始并停止我的听众 ? 我正在阅读this书,但我找不到明确的解释。在示例中,作者在设置函数中设置了侦听器,但我想知道为什么?是不是更好的想法在激活功能中设置它们并根据模板的注释建议将它们停用在停用功能中?

如果我错了什么events应该而且不应该放入设置和激活功能?

如果准确设置,激活,取消激活,清除功能会被调用吗?

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);
};

1 个答案:

答案 0 :(得分:5)

创建场景时会调用场景助手的设置,当它从堆栈弹出时会调用清理。在设置中,控件的实际HTML内容不可用,因为尚未处理场景的模板。在模板处理完成后,如果可用,则调用就绪方法,这是进行任何其他HTML DOM更改的好地方。在场景变为活动状态之前调用激活,而在弹出场景或在此场景之上推送另一个场景时调用 deativate 激活 / * 停用 *也会在应用最小化到卡片或带回全屏时调用。

通常最好在启动/停用时启动和停止事件监听器 - 将其活动时间保持在最低限度,并且更少的活动监听器使响应更快的系统。