如何声明和监听自定义事件?

发布于 2024-10-05 04:37:06 字数 326 浏览 9 评论 0原文

开发环境:HP/Palm WebOS、带有 SDK 1.4.5.465 的 Eclipse、Win7

我有一个类,我想在其中声明并在某些情况下触发一个事件。然后,在相应的阶段助手中侦听该事件,并在引发该事件时执行某些操作。

阅读参考资料时,我遇到了 Mojo.Event.make、Mojo.Controller.stageController.sendEventToCommanders、Mojo.Event.send 以及其他一些我认为与我想要实现的目标相关的内容,但我找不到一个具体的例子(声明、触发和监听)。

澄清一下,我想要触发的事件与小部件或带有 id 的 html 标签无关。

Developing environment: HP/Palm WebOS, Eclipse with SDK 1.4.5.465, Win7

I have a class in which I want to declare and under certain circumstances, fire an event. Then, in the corresponding stage assistant listen for that event, and when it's raised, do something.

reading the reference i've come across Mojo.Event.make, Mojo.Controller.stageController.sendEventToCommanders, Mojo.Event.send and a few more that i think are related to what i'm trying to achieve, but I fail to find an example especific to this (declaring, firing and listening).

To clarify, the event I want to fire is not related to a widget nor an html tag with an id.

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

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

发布评论

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

评论(1

暖风昔人 2024-10-12 04:37:06

Mojo.Event 依赖于 HTML 文档中的节点/元素作为事件的发起者。据我所知,没有针对 DOM 上下文之外的事件的内置库,因此此时您必须实现自己的库。根据您的情况有多复杂,您也许可以找到一种方法,只需在您正在侦听的对象上创建一个属性并存储一个在将来某个时间调用的函数:

ListeningObject = Class.create({
   initialize:function(){
     // instantiate instance of Subject
     var subject = new Subject();

     // set the onEvent property of subject to an instance of this.onEvent bound to 
     // a this instance of Listening object's context.
     subject.onEvent = this.onEvent.bind(this);

     subject.doSomethingAwesome();
   },
   onEvent:function(){
    Mojo.Log.info("This get's called from the object we're listening to");
   }
});

Subject = Class.create({
   doSomethingAwesome:function(){
     // does stuff, maybe an ajax call or whatever
     // when it's done you can check if onEvent is a function and then
     // you can call it, we'll use setTimeout to simulate work being done
     setTimeout((function(){
       if(Object.isFunction(this.onEvent)) this.onEvent();
     }).bind(this), 200);
   },
   onEvent:null
});

// instantiate an instance of ListeningObject to see it in action
var listening_object = new ListeningObject;

此模型的最大限制是您只能让一个对象监听特定事件,但在某些情况下这就是您所需要的。

Mojo.Event relies on the originator of the event being a node/element in an HTML document. From what I can tell there is no built in library for events outside of the DOM context so at this point you'll have to implement your own. Depending on how complex your situation is you may be able to get a way with just creating a property on the object you're listening to and storing a function that get's called a certain time in the future:

ListeningObject = Class.create({
   initialize:function(){
     // instantiate instance of Subject
     var subject = new Subject();

     // set the onEvent property of subject to an instance of this.onEvent bound to 
     // a this instance of Listening object's context.
     subject.onEvent = this.onEvent.bind(this);

     subject.doSomethingAwesome();
   },
   onEvent:function(){
    Mojo.Log.info("This get's called from the object we're listening to");
   }
});

Subject = Class.create({
   doSomethingAwesome:function(){
     // does stuff, maybe an ajax call or whatever
     // when it's done you can check if onEvent is a function and then
     // you can call it, we'll use setTimeout to simulate work being done
     setTimeout((function(){
       if(Object.isFunction(this.onEvent)) this.onEvent();
     }).bind(this), 200);
   },
   onEvent:null
});

// instantiate an instance of ListeningObject to see it in action
var listening_object = new ListeningObject;

The biggest limitation to this model is that you can only have one object listening to the particular event but in some situations that's all you need.

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