addyosmani/essential-js-design-patterns — Issue #13: PubSub example #4 - subscribe callbacks require event as first argument
Author: timkg
Created: 2012-03-03 23:26:45+00:00
State: open

Hi,

I believe there is an error in example #4, "Decoupling applications using Ben Alman's pub/sub implementation". Ben Alman's subscribe callbacks require the event object as first argument. His example states this specifically:

// taken from https://gist.github.com/661855
function handle(e, a, b, c) {
// `e` is the event object, you probably don't care about it.
console.log(a + b + c);
};

The handler functions given in your example lack this first argument:

// taken from http://addyosmani.com/resources/essentialjsdesignpatterns/book/#detailedobserver
$.subscribe( "/new/user", function( userName ){
// ...
});
$.subscribe( "/new/rating", function( movieTitle, userRating ){
// ...
});

When we enter those functions, the first argument gets overwritten by the jQuery event object. It should be:

$.subscribe( "/new/user", function( e, userName ){
// ...
});
$.subscribe( "/new/rating", function( e, movieTitle, userRating ){
// ...
});