自定义事件(观察者模式)
我找不到如何实现这个(我为自由式记录道歉):
//assign the event handler for the object "myObj"
myObj.onMyEvent = //do something
//if something happened somewhere, then run the event
MyEvent.fire();
这个想法是我们调用(生成)一些自定义事件(MyEvent)。我们有一个对象“myObj”,它能够识别“MyEvent”的发生并以某种方式(以他自己的方式)对其做出响应。 此外,我们还有其他对象(不同类型),它们(以自己的方式)对“MyEvent”做出反应。
我知道有“观察者模式”,但我找不到具体的实现。
你能告诉我在哪里可以读到它吗? 或者说本质上一般是不可能的? 或者它的做法完全不同?
更新 1:请不要使用 JS 库(jQuery、Prototype、YUI,...)
I can not find how to implement this (I apologize for the freestyle record):
//assign the event handler for the object "myObj"
myObj.onMyEvent = //do something
//if something happened somewhere, then run the event
MyEvent.fire();
The idea is that we call (generate) some custom event (MyEvent). We have an object "myObj" that is able to recognize the occurrence of the "MyEvent" and somehow (in his own way) to respond to it.
In addition, we have other objects (diffrent sorts), that (in their own way) react to "MyEvent".
I know that there are "observer pattern" but I can not find a specific implementation.
Can you please tell where to read about it?
Or in nature is generally not possible?
Or it is done quite differently?
Update 1: no JS libs, please (jQuery, Prototype, YUI, ...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会认真地重新考虑这个要求。所有这些库都多次解决了这个或类似的问题。
但如果您想从头开始,请执行以下操作:
用法:
I would seriously re-think this requirement. All of these libraries have solved this or similar problems many times.
But if you want to start from scratch, do something like this:
Usage:
我的解决方案:
My solution: