jQuery tiny PubSub is great when passing primitive values or objects, but has some trouble with arrays. So I have to wrap arrays into an object.
(function($) {
var o = $({});
$.subscribe = function() {
o.on.apply(o, arguments);
};
$.unsubscribe = function() {
o.off.apply(o, arguments);
};
$.publish = function() {
o.trigger.apply(o, arguments);
};
}(jQuery));
$.subscribe('test',function(e,data){
console.log(data);
})
$.publish('test',1); //1
$.publish('test',{a:1}); //{a:1}
$.publish('test',[2,3,4]); //2
$.publish('test',{arr:[2,3,4]}) //{arr:[2,3,4]}
I’ve seen some improve versions of it, which mainly focus on caching subscribers, but none of them can pass arrays. So, two questions:
- Is it a good idea to pass arrays via PubSub?
- How to do that?
Alright, I figure it out anyway.
Even thought it may not be a problem to others, but not being able to pass arrays via
PubSubis very confusing and inconvenient to me. So I decide to write my ownPubSub, instead of using jQuery’s custom events.