PDA

View Full Version : Poor-man's Ext.ux.MsgBus



hendricd
1 Aug 2008, 9:44 AM
(function(){

Ext.extend(
Ext.ux.MsgBus = function(config){
this.events = {};
Ext.apply(this,config||{});
Ext.ux.MsgBus.superclass.constructor.call(this);
},
Ext.util.Observable,
{
publish : function(topic /* ,variable arguments ,,, */ ){
var t= String(topic);
this.events[t] || (this.addEvents(t));
return this.fireEvent.apply(this, [t].concat(Array.prototype.slice.call(arguments,1)));
}

});
var uxp = Ext.ux.MsgBus.prototype;
Ext.apply(uxp,{
subscribe : uxp.on, //alliases
unsubscribe : uxp.un,
destroy : uxp.purgeListeners
});

})(); Usage:

Ext.ns('your');
your.MessageBus = new Ext.ux.MsgBus();

your.MessageBus.subscribe('test',
function(){ console.log(arguments); },
context,
{single:true, delay: 500 } //standard Observable arguments and options
);

var wasCancelled = (your.MessageBus.publish('test', 'this is only a test', someObj, someArray) === false);
License: LGPL 3.0 or higher.

shane.fox
3 Aug 2008, 7:44 PM
Nice work. This is one of the things I missed when I switched from Dojo.

Yoris
5 Aug 2008, 5:57 PM
i just don

mystix
5 Aug 2008, 5:58 PM
[QUOTE=Yoris;204948]i just don

brian.moeskau
5 Aug 2008, 8:57 PM
Yeah, this is something we've been considering adding into Ext. Would be pretty useful in many situations for keeping component coupling to a minimum.

fangzhouxing
5 Aug 2008, 9:33 PM
Yeah, this is something we've been considering adding into Ext. Would be pretty useful in many situations for keeping component coupling to a minimum.

I have used PageBus for long time to achive similar function.

mabello
6 Aug 2008, 12:06 AM
Hi there,
This is something that I tried to achieve with the mediator pattern:
http://extjs.com/forum/showthread.php?t=31597&highlight=Mediator
But I wrote that 3 years ago, but I think that the idea is the same...it is Ext agnostic, because Ext was not born yet...
In my implementation, the Mediator is a singleton and keep the reference of all the objects that subscribed for an event, so that you can query the mediator to understand better who is the subscriber of an event and simplify the understending of the system when it "runs".

ThorstenSuckow
6 Aug 2008, 12:33 AM
There is another implementation over at http://code.google.com/p/ext-ux-util-messagebus/ .


hendric, don't want to nitpick, but is there a reason why you didn't use the singleton pattern here?



Edit:
Who's poor-man?!

SamuraiJack1
6 Aug 2008, 1:51 AM
Seems like copy of my Ext.ux.BroadcastEvents, even method names are the same http://extjs.com/forum/showthread.php?t=37422

ThorstenSuckow
6 Aug 2008, 2:01 AM
Seems like copy of my Ext.ux.BroadcastEvents, even method names are the same http://extjs.com/forum/showthread.php?t=37422

I hope you do not accuse me of stealing IP from you ;) The api of a Message Broker is pretty obvious, isn't it?

Further reference:

http://msdn.microsoft.com/en-us/library/ms978583.aspx

and Tibcos Page bus:

http://www.tibco.com/devnet/pagebus/default.jsp

SamuraiJack1
6 Aug 2008, 2:21 AM
I hope you do not accuse me of stealing IP from you ;) The api of a Message Broker is pretty obvious, isn't it?


No, that ux wasnt licensed ))

SamuraiJack1
6 Aug 2008, 2:34 AM
And I was talking about Ext.ux.MsgBus (http://extjs.com/forum/showthread.php?p=205170#post205170), not your link )

mjlecomte
6 Aug 2008, 4:14 AM
I see there's a pagebus in the distro nowj (check the core folder). Doesn't do anything though. Something on the down lo perhaps.

hendricd
6 Aug 2008, 5:02 AM
First off, I'll point out that this mere few lines of code was assembled in response to a Forum Help thread (http://extjs.com/forum/showthread.php?p=228674#post228674)on how to communicate in a loosely-coupled fashion between two objects.

With the existing knowledge that Ext.util.Observable already provided all the basic elements of (the well-known, widely publicized) "publish/subscribe", I literally slapped this together in about 2 minutes to demonstrate a solution without having to go outside Ext to provide it. It was suggested I post it over here so people could find it, so I did.


There is another implementation over at http://code.google.com/p/ext-ux-util-messagebus/ .


hendric, don't want to nitpick, but is there a reason why you didn't use the singleton pattern here?
@MindPatterns -- My typical preference for Ext additions is classes. In this case, the class could be further extended to add other features like (debugging aids, queueing, server logging, XHR request support, who knows?). A singleton might make that more difficult. In the end, as the usage example shows, the implementor can simply instantiate a base or subclass for just such purposes (flexibility).


Edit: Who's poor-man?!One of those American euphamisms indicating: quick, dirty, and inexpensive.


Seems like copy of my Ext.ux.BroadcastEvents, even method names are the same http://extjs.com/forum/showthread.php?t=37422@SamuraiJack1 -- Sorry if the well-known method names (publish, subscribe) implies IP infringement, but my motives were limited to using well-known 'verbs', that developers have used for years, to create a simple self-documenting class that applies these 'now-ancient' concepts to help someone other than myself.
I'll happily withdraw it, if you feel that extending Observable and renaming a couple of methods is something truly unique to your implementation. /:)


No, that ux wasnt licensed ))I slapped a (seemingly Ext-compatible) license on there because many ask for it when interested in using something they see on the Forums. All extension developers should do the same. It adds a bit of clarity to the landscape, no?

I guess I expected that the 'ux.clash' would begin soon, and it has.
This is a good reason why some sort of central ux Registry is desirable. But, I think its a bit much to ask the many active Forum contributors to analyze all the available code before offering (frankly) simple code fragments like this one, to help people out.

Just wait until JIT-wars begin. :-?

mystix
6 Aug 2008, 5:12 AM
Just wait until JIT-wars begin. :-?

http://img98.imageshack.us/img98/2447/lightsaber11ff3.jpg

en garde (http://www.guerrestellari.it/slas.html). :))

mjlecomte
6 Aug 2008, 5:40 AM
damn if I just didn't waste about 15 min of my life on that one. Awesome. I had my speakers off and was making my own sound effects. I'm dum.

mystix
6 Aug 2008, 5:44 AM
damn if I just didn't waste about 15 min of my life on that one. Awesome. I had my speakers off and was making my own sound effects. I'm dum.

now if only you'd web-cammed yourself doing the saber sounds -- you'd definitely outdo the numa numa dude (http://www.youtube.com/watch?v=60og9gwKh1o). :))

SamuraiJack1
6 Aug 2008, 5:48 AM
I'll happily withdraw it, if you feel that extending Observable and renaming a couple of methods is something truly unique to your implementation. /:)

Nevermind, if broadcasting will be added to ext core because of this thread, I'll even say thanks to you.



Just wait until JIT-wars begin. :-?

Your turn to release )

mjlecomte
6 Aug 2008, 5:58 AM
now if only you'd web-cammed yourself doing the saber sounds -- you'd definitely outdo the numa numa dude (http://www.youtube.com/watch?v=60og9gwKh1o). :))

Another 1:38 of my life gone to waste. LMAO. I didn't have the enthusiasm he did. But I think my sounds were a bit better than the lightsabre's site.

Doug, maybe your media panel can wire up a Ext.ux.LightSabre component. Who needs clicking when you could light sabre some stuff?

hendricd
6 Aug 2008, 6:00 AM
Your turn to release )

Demo site for it (http://extjs.com/forum/showthread.php?p=184817#post184817) should be up soon.

hendricd
6 Aug 2008, 6:01 AM
Doug, maybe your media panel can wire up a Ext.ux.LightSabre component. Who needs clicking when you could light sabre some stuff?

Ha :)). Gotta solve the power problem first.