我正在启动 WebOS 开发,但我对应该在哪里开始和停止我的听众有疑问?我正在阅读这本书,但我找不到明确的解释。在示例中,作者在 setup 函数中设置了侦听器,但我想知道为什么?将它们设置为激活功能并按照模板注释的建议将它们停止在停用功能中不是更好的主意吗?
如果我错了,应该和不应该在设置和激活功能中放置什么样的事件?
何时调用设置、激活、停用、清理功能?
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);
};