Hello,
I wanted to propose a small class that would help to overcome the
feature that's missing in MSIE. I'd like to get some feedback from
people and, perhaps, improvements in code/other ideas:
/*************** *************** *************** *************** *************** *************** *
DCListener class
Description: provides basic event listener functionality
Interface :
contructor DCListener(obj, evt)
public void dispatch(evt)
public int addSubscriber(f nSubscriber)
public bool removeSubscribe r(id)
public void unregister()
public static int id
public static {DCListener} index
*************** *************** *************** *************** *************** *************** */
function DCListener(obj, evt) {
var id = DCListener.id++ ;
this.id = id;
this.obj = obj;
this.evt = evt;
this.subscriber s = {};
this.subId = 0;
this.save = obj.evt;
var listenerId = this.id;
obj[evt] = function() {
DCListener.inde x[listenerId].dispatch();
}
DCListener.inde x[id] = this;
}
DCListener.prot otype.dispatch = function(evt) {
var subscribers = DCListener.inde x[this.id].subscribers;
for (var sub in subscribers) subscribers[sub](evt);
}
DCListener.prot otype.addSubscr iber = function(fnSubs criber) {
var id = this.subId++;
this.subscriber s[id] = fnSubscriber;
return id;
}
DCListener.prot otype.removeSub scriber = function(id) {
if (id in this.subscriber s) {
delete this.subscriber s[id];
return true;
} else {
return false;
}
}
DCListener.prot otype.unregiste r = function() {
delete DCListener.inde x[this.id];
this.obj[this.evt] = this.save;
delete this.id;
delete this.obj;
delete this.evt;
delete this.subscriber s;
delete this.subId;
delete this.save;
}
DCListener.id = 0;
DCListener.inde x = {};
********** end of code **********
Usage:
var lstDocumenOnCli ck = new DCListener(docu ment, "onclick");
var idA = lstDocumenOnCli ck.addSubscribe r(function() {
alert("[A] received document.onclic k");
});
var idB = lstDocumenOnCli ck.addSubscribe r(function() {
alert("[B] received document.onclic k");
});
// Later, to remove the first subscriber:
lstDocumenOnCli ck.removeSubscr iber(idA);
*************** *************** **********
This is just starting draft, and I know it needs some things to be
done about:
1) Passing the event object to every subscriber
2) Perhaps syncing the method naming with Mozilla event
listener/subscriber model.
3) Testing for memory leaks
Please, I am open to ideas, and I hope this thingy might be useful for
JS community.
Regards,
Pavils
I wanted to propose a small class that would help to overcome the
feature that's missing in MSIE. I'd like to get some feedback from
people and, perhaps, improvements in code/other ideas:
/*************** *************** *************** *************** *************** *************** *
DCListener class
Description: provides basic event listener functionality
Interface :
contructor DCListener(obj, evt)
public void dispatch(evt)
public int addSubscriber(f nSubscriber)
public bool removeSubscribe r(id)
public void unregister()
public static int id
public static {DCListener} index
*************** *************** *************** *************** *************** *************** */
function DCListener(obj, evt) {
var id = DCListener.id++ ;
this.id = id;
this.obj = obj;
this.evt = evt;
this.subscriber s = {};
this.subId = 0;
this.save = obj.evt;
var listenerId = this.id;
obj[evt] = function() {
DCListener.inde x[listenerId].dispatch();
}
DCListener.inde x[id] = this;
}
DCListener.prot otype.dispatch = function(evt) {
var subscribers = DCListener.inde x[this.id].subscribers;
for (var sub in subscribers) subscribers[sub](evt);
}
DCListener.prot otype.addSubscr iber = function(fnSubs criber) {
var id = this.subId++;
this.subscriber s[id] = fnSubscriber;
return id;
}
DCListener.prot otype.removeSub scriber = function(id) {
if (id in this.subscriber s) {
delete this.subscriber s[id];
return true;
} else {
return false;
}
}
DCListener.prot otype.unregiste r = function() {
delete DCListener.inde x[this.id];
this.obj[this.evt] = this.save;
delete this.id;
delete this.obj;
delete this.evt;
delete this.subscriber s;
delete this.subId;
delete this.save;
}
DCListener.id = 0;
DCListener.inde x = {};
********** end of code **********
Usage:
var lstDocumenOnCli ck = new DCListener(docu ment, "onclick");
var idA = lstDocumenOnCli ck.addSubscribe r(function() {
alert("[A] received document.onclic k");
});
var idB = lstDocumenOnCli ck.addSubscribe r(function() {
alert("[B] received document.onclic k");
});
// Later, to remove the first subscriber:
lstDocumenOnCli ck.removeSubscr iber(idA);
*************** *************** **********
This is just starting draft, and I know it needs some things to be
done about:
1) Passing the event object to every subscriber
2) Perhaps syncing the method naming with Mozilla event
listener/subscriber model.
3) Testing for memory leaks
Please, I am open to ideas, and I hope this thingy might be useful for
JS community.
Regards,
Pavils
Comment