On 3/26/2011 10:51 AM, BlessYAHU wrote:
I know one can use deferreds and promises when doing ajax calls, but
can these be used for custom events? Is pubsub the best way to have
decoupled components talk to each other?
It seems to me that deferreds work well for success/failure, but you could add
in your own custom events or simply publish an event from within the success or
failure of your deferred.
One thing that I've noticed, at least with the way that deferreds are
implemented in jQuery ajax calls, is that if you add a success handler after
the deferred has completed successfully, that the newly added success handler
will be executed. I suppose the same would happen for failures. I was only
testing to see if it that would happen as I hadn't seen any documentation or
any mention in the flurry of blogs post and tweet about deferreds getting added
to jQuery about what happens if you add handlers after the success or failure
had occurred. So you should consider how that might impact what you're trying
to accomplish with your own code if events can keep being published when you
add a new handler after the event was triggered if most deferred
implementations work in a similar way.
Only thing I think is missing from pubsub is having a way to know
which event a component subscribes/publishes.
Read the documentation, or read the code is how I've done it in the past. I
suppose you could always add your own methods that provides a list of
subscriptions available, or publications available, or provide an access method
that let's you know if an event is subscribable (sp?) or publishable. It's
possible that your object(s) may want to find out if they can subscribe of not
(due to what events they respond to), but that could be clunky as you'll always
be checking before you subscribe (something similar to):
if(obj.publishes('eventName')){
obj.subscribe('eventName', obj2);
}
Maybe I haven't been exposed to enough, but I don't think I've seen a pubsub
implementation that actually checked to see if the event being subscribed to
was something that the object actually published, much less enforce or limit
the events that are attempted to be subscribe to. Given the nature of pubsub,
it shouldn't matter. If you subscribe to an event that will never be published,
then nothing will happen, same with publishing an event that nothing is
subscribed to, nothing happens.
Danilo Celic
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]