如何声明和监听自定义事件?
开发环境: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Mojo.Event 依赖于 HTML 文档中的节点/元素作为事件的发起者。据我所知,没有针对 DOM 上下文之外的事件的内置库,因此此时您必须实现自己的库。根据您的情况有多复杂,您也许可以找到一种方法,只需在您正在侦听的对象上创建一个属性并存储一个在将来某个时间调用的函数:
此模型的最大限制是您只能让一个对象监听特定事件,但在某些情况下这就是您所需要的。
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:
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.