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

发布于 2024-09-28 17:20:30 字数 1537 浏览 2 评论 0原文

我正在启动 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);
};

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌︼了一个春 2024-10-05 17:20:30

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

通常最好在激活/停用时启动和停止事件侦听器,这样可以将事件侦听器的活动时间降至最低,并且较少的活动侦听器可以使系统响应更快。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文