I've implemented the observer pattern in JS in a very universal way. Now I realized that there is a conceptional problem and I don't know how to solve it. Could you give me a tipp how to solve it? (particularly in a very smart way :-) ) This is the code:
var Oberserverable = function() {
var subscribers = { /* some delegated functions in here */ };
return {
attach: function (obj, fn) { subscribers[obj] = { src: obj, fn: fn }; },
detach: function (obj) { delete subscribers[obj]; },
notify: function (args) {
for(var o in subscribers) {
if(typeof args === "object") { args.target = subscribers[o].src; }
/* TODO return */ subscribers[o].fn.call(window, args);
}
},
count: function () { var size = 0; for(var o in subscribers) { size++; } return size; }
};
};
I'd like that each delegated function returns its value. But this would end the loop what I'd like to prevant.
Example case where I'm using it; workaround for crappy IE8 Eventhandeling:
window.addEvent = function (obj, type, fn, bub) {
if(obj.addEventListener) {
return obj.addEventListener(type, fn, bub ? bub : false);
}
if(obj.attachEvent && type == "DOMContentLoaded") type = "load";
// no use of attachEvent() 'cause of very buggy behaviour in IE<=8
// http://stackoverflow.com/questions/15952943/ie-attachevent-call-returns-true-but-handler-shows-null
if(!obj["e"+type]) obj["e"+type] = new Oberserverable();
obj["e"+type].attach("e"+obj+fn, fn);
if(!obj["on"+type]) {
obj["on"+type] = function(e) {
e = e || window.event;
e.cancelBubble = bub;
return obj["e"+type].notify(e) || (type != "contextmenu"); // suppressing context menu in IE8 as default
};
}
}