amplify icon indicating copy to clipboard operation
amplify copied to clipboard

Subscribe to multiple pubs with the same function

Open iangilman opened this issue 13 years ago • 9 comments

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().

iangilman avatar Jan 12 '12 21:01 iangilman

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); 
});

ifandelse avatar Jan 12 '12 21:01 ifandelse

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

iangilman avatar Jan 12 '12 22:01 iangilman

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

iangilman avatar Jan 12 '12 22:01 iangilman

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....

ifandelse avatar Jan 12 '12 22:01 ifandelse

Awesome, thanks!

iangilman avatar Jan 12 '12 23:01 iangilman

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

mgiulio avatar Jan 20 '12 09:01 mgiulio

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

ifandelse avatar Jan 20 '12 16:01 ifandelse

@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.

ifandelse avatar Jan 20 '12 16:01 ifandelse

@ifandelse Excellent!

iangilman avatar Jan 23 '12 19:01 iangilman