TwelvestoneProjects and Theory

JQuery broadcaster


Sign in

  • Waiting for Godot ( 720 k posts )
    Just conversation.
  • Thunder Dome ( 23 k posts )
    Photoshop Tennis and Collabs.
  • Photography ( 4.8 k posts )
    For all you shutterbugs, sh...
  • Flash ( 18 k posts )
    ActionScripting to tweens, ...
  • Front End ( 5.8 k posts )
    general front end design an...
  • Back End ( 9.6 k posts )
    serverside scripting, progr...
  • Projects and Theory ( 12 k posts )
    This forum is for discussio...
  • FAQ ( 269 posts )
    All those nagging questions...
  • Design ( 17 k posts )
    graphics & all aspects of g...
  • Purgatory ( 3.6 k posts )
    12stone Jail, feel free to ...
Stinky
 
2010-01-05

Here's a little plugin I wrote the other night for JQuery to implement a simple subscriber/publisher for arbitrary Javascript events http://github.com/weeksie/jquery_broadcaster

So you can do stuff like this:

$("#listeningelement").register("new_message", function(, data) { $(this).html(data); });

$.get("/url", { success: function(data) { $.broadcast("new_message", data); } });

So if you have a buttload of elements on a page that need to be updated when an Ajax call (or other event) returns you can handle them nice and seamlessly. And stuff.

persist
 
2010-01-05

I can't think of any slimmer way to do this. No other method allows you to bind individual elements to a single event type with a custom handler on each. This would be handy on an app with multiple changes across the ui with a single data model change. It allows for complete decoupling of a model change in ajax, which is pretty nifty.

Nice and tidy.

:grumpy:

Stinky
 
2010-01-05

Thanks k

It struck me as odd that there weren't any other libs around for that. I'm sure I'm missing something but this seemed like a nice and tight little implementation anyway so I figured I'd share (or at least get pointed in the direction of the standard method for doing this).

persist
 
2010-01-05

well $.event.trigger should work, since its global, but then, it's global. It's also jquery internal and not documented for us to use. So it might break? :P

$('#test1').bind('new_message', function(){ alert("ok1"); });

$('#test2').bind('new_message', function(){ alert("ok2"); });

$.get("/url", { success: function(data) { $.event.trigger('new_message', data); }});

persist
 
2010-01-05

I take that back, you also need to populate event with the variables you want to pass to the handler, so the example wouldn't work. You can only pass trigger a single param. :P

Stinky
 
2010-01-05

Yeah, it seems like I tried that route first but it wouldn't work for some reason.

persist
 
2010-01-05

I can only get it to work if i set up a document level container for the data. But then I don't know when to destroy it cause who knows who is listening and may still need it?

your method should be part of jquery. it makes beautiful sense. k

Candy Beard
 
2010-01-05

I wish I understood anything you two sexy beasts were saying.

JLM
 
2010-01-06

I think they are talking about setting up listeners, or something... I would be interested in there thoughts on Pimm's HSL library, I believe being haXe it can be applied in javascript but don't really follow if its relvent to the above, but I am thinking about using it in haXe/as3 in the future, I am sure it is heavier but sometimes in software engineering you have to work with more abstract concepts to manage complexity and develop faster, I don't know maybe my belief in haXe is unfounded.

svn:http://hxhsl.googlecode.com/svn/branches/draft2/library/ previous docs:http://code.google.com/p/hxhsl/wiki/PrefaceAndIntroduction

Stinky
 
2010-01-07

Originally posted by: JLM I think they are talking about setting up listeners, or something...

Yes.

. . . it is heavier but sometimes in software engineering you have to work with more abstract concepts to manage complexity and develop faster

No.

Most of the time when you have something that is fascinatingly complex it can be done simpler. Do it that way.

the real me
 
2010-01-07

Man, where was this 6 months ago! This could have saved me so much time on a super ajaxifidian contest page thing I built. ( i suck with the javascripts)

I've been playing around with webhooks the last few days and i think i can see myself making use of this.

Stinky
 
2010-01-07

Let me know if you have any problems with it k

persist
 
2010-01-08

Hey thanks for adding a license. makes it simpler to use here.

Stickman
 
2010-02-03

Sweet. :thumbsup:

Only problem I can see is the possibility of event name duplication -- potentially a problem if you were integrating, say, an external library that happened to use the same listener/broadcaster technique. Argues for namespacing your event names.

Stinky
 
2010-02-03

Good call, I added namespacing and some tests k

arigato
 
2010-02-04

Hm, that sounds oddly useful. k

Stickman
 
2010-02-04

Hmm..just tested it and I get an error when an event is broadcast that has no listeners (because $._subscribers[evt] is not defined).

Also, how do I set the namespace per event? So let's say I have two libraries, each broadcasting a 'save' message. I want to register a listener for the first library but not the second. So I need to specify the namespace when calling register(). Then I also need to set the namespace when calling broadcast(), so that only listeners to that namespace are triggered.

It might be simpler to forget explicit broadcaster functionality and just suggest that developers prepend the message name with a namespace (e.g. lib_1.save).

Apologies if I've missed something obvious.

Stinky
 
2010-02-04

Good catch with the bug there, I'll add a test case and a bugfix this afternoon k

Regarding namespacing, I just added that so it wouldn't conflict with other libraries or built in events. If you're registering your own you'll still want to namespace them manually.

Stinky
 
2010-02-04

Yeah, I pushed that fix up, yo.

persist
 
2010-02-05

I am trying to wrap my head around using a static like this.

I think you want:

(function($) {

$.extend ({ BroadCaster:{ namespace:"com.twelvestone" } });

...

I dunno if this even works. I am just trying to understand why we'd need to use extend instead of plopping something right on jquery.

?

persist
 
2010-02-05

anyways. e if plopping objects directly on $ isn't good.

$._GET = {};

Stinky
 
2010-02-05

You're totally right regarding the extend thing. I was just being retarded k

persist
 
2010-02-05

But in your knowledge, do you understand why this is best practice?

Like is jquery doing anything smart under the covers in terms of memory or garbage collection or?

Stinky
 
2010-02-05

I was half thinking about making a config function but then decided afterward that it made sense to just stick the namespace variable in a hash on jQuery. If it was a config function and I had more than one option it would have made sense to use the extend function so I could leave sensible defaults . . . Basically I was halfway through a thought and then scrapped it but forgot to clean up after myself k

So yeah, long story short there's no whackness at all around sticking something right onto jQuery in the context that you're doing it.

Stickman
 
2010-02-08

Hey Stinky...

On a tangential note, I notice you've specified the code as dual-licensed (MIT and GPL) and was wondering why. I've noticed other projects do this, and don't really understand the reasoning, since it doesn't seem to be to do with commercial/non-commercial usage.

TwelvestoneProjects and Theory

JQuery broadcaster