admin管理员组文章数量:1318572
I want to simulate C#'s events in JavaScript: what i want to do is something like this:
Let's say i have the following code:
function addToInvocationList(method, listener) {
*Some code to add listener to the invocation list of method*
}
function MyClass() {
}
MyClass.prototype.Event = function() {}
var my_class_obj = new MyClass();
function subscriberFunction1() {}
function subscriberFunction2() {}
function subscriberFunction3() {}
addToInvocationList(my_class_obj.Event, subscriberFunction1);
addToInvocationList(my_class_obj.Event, subscriberFunction2);
addToInvocationList(my_class_obj.Event, subscriberFunction3);
my_class_obj.Event();
What i want to do is when i call my_class_obj.Event, all the subscribed functions get called.
Could this be achieved purely in JavaScript or i need to find my way around through DOM events?
I want to simulate C#'s events in JavaScript: what i want to do is something like this:
Let's say i have the following code:
function addToInvocationList(method, listener) {
*Some code to add listener to the invocation list of method*
}
function MyClass() {
}
MyClass.prototype.Event = function() {}
var my_class_obj = new MyClass();
function subscriberFunction1() {}
function subscriberFunction2() {}
function subscriberFunction3() {}
addToInvocationList(my_class_obj.Event, subscriberFunction1);
addToInvocationList(my_class_obj.Event, subscriberFunction2);
addToInvocationList(my_class_obj.Event, subscriberFunction3);
my_class_obj.Event();
What i want to do is when i call my_class_obj.Event, all the subscribed functions get called.
Could this be achieved purely in JavaScript or i need to find my way around through DOM events?
Share Improve this question edited Sep 9, 2013 at 13:32 Khanh TO 49k13 gold badges101 silver badges116 bronze badges asked Apr 12, 2013 at 5:48 hiddenUserhiddenUser 6921 gold badge8 silver badges20 bronze badges2 Answers
Reset to default 9How about writing a separate event class: Reference link: http://alexatnet./articles/model-view-controller-mvc-javascript
function Event(sender) {
this._sender = sender;
this._listeners = [];
}
Event.prototype = {
attach: function (listener) {
this._listeners.push(listener);
},
notify: function (args) {
for (var i = 0; i < this._listeners.length; i++) {
this._listeners[i](this._sender, args);
}
}
};
And your my class. For example:
function MyClass(name) {
var self = this;
self.Name = name;
self.nameChanged = new Event(this);
self.setName = function (newName){
self.Name = newName;
self.nameChanged.notify(newName);
}
}
Subscribe to event example code:
var my_class_obj = new MyClass("Test");
my_class_obj.nameChanged.attach(function (sender,args){
});
my_class_obj.setName("newName");
You can attach more event handlers and all these event handlers will get called. And you can also add more events as you'd like: addressChanged event for example. This approach also simulate c# event (sender and args)
You could write your own Observer (publisher-subscriber) read the GOF. Or, if using jQuery, custom events (http://api.jquery./trigger/) can help you out.
本文标签: Simulate C like events in javascriptStack Overflow
版权声明:本文标题:Simulate C# like events in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742047861a2417895.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论