P
Pavils Jurjans
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(fnSubscriber)
public bool removeSubscriber(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.subscribers = {};
this.subId = 0;
this.save = obj.evt;
var listenerId = this.id;
obj[evt] = function() {
DCListener.index[listenerId].dispatch();
}
DCListener.index[id] = this;
}
DCListener.prototype.dispatch = function(evt) {
var subscribers = DCListener.index[this.id].subscribers;
for (var sub in subscribers) subscribers[sub](evt);
}
DCListener.prototype.addSubscriber = function(fnSubscriber) {
var id = this.subId++;
this.subscribers[id] = fnSubscriber;
return id;
}
DCListener.prototype.removeSubscriber = function(id) {
if (id in this.subscribers) {
delete this.subscribers[id];
return true;
} else {
return false;
}
}
DCListener.prototype.unregister = function() {
delete DCListener.index[this.id];
this.obj[this.evt] = this.save;
delete this.id;
delete this.obj;
delete this.evt;
delete this.subscribers;
delete this.subId;
delete this.save;
}
DCListener.id = 0;
DCListener.index = {};
********** end of code **********
Usage:
var lstDocumenOnClick = new DCListener(document, "onclick");
var idA = lstDocumenOnClick.addSubscriber(function() {
alert("[A] received document.onclick");
});
var idB = lstDocumenOnClick.addSubscriber(function() {
alert(" received document.onclick");
});
// Later, to remove the first subscriber:
lstDocumenOnClick.removeSubscriber(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(fnSubscriber)
public bool removeSubscriber(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.subscribers = {};
this.subId = 0;
this.save = obj.evt;
var listenerId = this.id;
obj[evt] = function() {
DCListener.index[listenerId].dispatch();
}
DCListener.index[id] = this;
}
DCListener.prototype.dispatch = function(evt) {
var subscribers = DCListener.index[this.id].subscribers;
for (var sub in subscribers) subscribers[sub](evt);
}
DCListener.prototype.addSubscriber = function(fnSubscriber) {
var id = this.subId++;
this.subscribers[id] = fnSubscriber;
return id;
}
DCListener.prototype.removeSubscriber = function(id) {
if (id in this.subscribers) {
delete this.subscribers[id];
return true;
} else {
return false;
}
}
DCListener.prototype.unregister = function() {
delete DCListener.index[this.id];
this.obj[this.evt] = this.save;
delete this.id;
delete this.obj;
delete this.evt;
delete this.subscribers;
delete this.subId;
delete this.save;
}
DCListener.id = 0;
DCListener.index = {};
********** end of code **********
Usage:
var lstDocumenOnClick = new DCListener(document, "onclick");
var idA = lstDocumenOnClick.addSubscriber(function() {
alert("[A] received document.onclick");
});
var idB = lstDocumenOnClick.addSubscriber(function() {
alert(" received document.onclick");
});
// Later, to remove the first subscriber:
lstDocumenOnClick.removeSubscriber(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