mikehostetler / amplify

AmplifyJS
http://amplifyjs.com
GNU General Public License v2.0
1.45k stars 144 forks source link

Subscribe to multiple pubs with the same function #46

Open iangilman opened 12 years ago

iangilman commented 12 years ago

Sometimes I'd like to handle a number of similar topics with the same function. I'm imagining something like this:

amplify.subscribe(["foo", "bar", "baz"], function(data, topic) {
    alert("got a " + topic + " with " + data); 
});

For that matter, a handy feature for debugging and logging would be subscribeAll().

ifandelse commented 12 years ago

amplify.subscribe() can take a space-delimited list of topics currently, and will subscribe the callback for each topic. For example:

amplify.subscribe("foo bar baz", function(data) {
    alert("got some data " + data); 
});
iangilman commented 12 years ago

Perfect! Looks like it's a documentation issue then; thanks for adding the label.

iangilman commented 12 years ago

oh, wait... can you find out which topic you're receiving? Without that, it's a good deal less useful.

ifandelse commented 12 years ago

You probably noticed I revised my reply's code sample, since I, too, thought the callback args included topic - sorry about that. I'm chatting with the appendTo team to see about opening an issue for adding topic to the callback args....

iangilman commented 12 years ago

Awesome, thanks!

mgiulio commented 12 years ago

Yes, maybe the documentation should mention this. My code wasn't working because I assumed that I could use event names with spaces.

ifandelse commented 12 years ago

@mgiulio - I have a pull request pending that will update the documentation once it's merged.

ifandelse commented 12 years ago

@iangilman The pull request I've opened will update the documentation (the amplify.core README) with examples on how you could capture topic inside the subscription callback.

iangilman commented 12 years ago

@ifandelse Excellent!