Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bind multiple event-callback pairs in one "Backbone.Events.on" call #1456

Closed
wants to merge 2 commits into from

Conversation

gurdotan
Copy link

Instead of issuing lots of .on calls with different arguments, this change allows to pass a map of {event strings : callbacks} which will all be bound to the caller. A second context argument can be passed to determine this for all callbacks.

So instead of this:

var obj = _.extend({}, Backbone.Events);
obj.on("play", function() {...}, this);
obj.on("stop", function() {...}, this);
...

We can do this:

var obj = _.extend({}, Backbone.Events);
obj.on({
  "play" : function() {...},
  "stop" : funciotn() {...},
  ...
}, this);

It becomes especially handy if you're binding lots of functions. This style is influenced by jQuery 1.7's new .on API.

@refaelos
Copy link

Great stuff man!

@amitzur
Copy link

amitzur commented Jun 26, 2012

I think this is a great syntactic sugar...

@anthonyshort
Copy link

This is great! It follows the style of other backbone methods that provide similar, flexible parameters.

// First argument is a map of event names => handlers
// Second argument is a context object
context = callback;
var $this = this;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this $this was for scope referencing?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. This variable is redundant - I removed it in the next commit. Thanks!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pshhhhhh

Sent from my Android, please excuse any typos or mistakes.
On Jul 24, 2012 1:27 PM, "Gur Dotan" <
reply@reply.github.com>
wrote:

  •  return this;
    
  •    while (event = events.shift()) {
    
  •      list = calls[event] || (calls[event] = []);
    
  •      list.push(callback, context);
    
  •    }
    
  •    return this;
    
  •  }, this);
    
  •  if (_.isObject(events)) {
    
  •    // First argument is a map of event names => handlers
    
  •    // Second argument is a context object
    
  •    context = callback;
    
  •    var $this = this;
    

Good catch. This variable is redundant - I removed it in the next commit.
Thanks!


Reply to this email directly or view it on GitHub:
https://github.com/documentcloud/backbone/pull/1456/files#r1223619

@jashkenas jashkenas closed this in 362b91c Dec 6, 2012
@jashkenas
Copy link
Owner

Good idea. I've pushed a different implementation. Let me know if I'm missing anything.

@caseywebdev
Copy link
Collaborator

👍 Awesome!

@JoeChapman
Copy link

Nice one

@gurdotan
Copy link
Author

gurdotan commented Dec 6, 2012

Sweet implementation, hits the spot. Thanks!

@yanivnizan
Copy link

Great stuff dude!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants