Skip to content

Commit

Permalink
allowing view events to be functions instead of simply names of prope…
Browse files Browse the repository at this point in the history
…rties on a view
  • Loading branch information
Zack Owens committed Dec 25, 2011
1 parent a9fcd9b commit 1c15874
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion backbone.js
Expand Up @@ -930,6 +930,7 @@
// {
// 'mousedown .title': 'edit',
// 'click .button': 'save'
// 'click a': function (evnt) { ... }
// }
//
// pairs. Callbacks will be bound to the view, with `this` set properly.
Expand All @@ -941,7 +942,8 @@
if (!(events || (events = getValue(this, 'events')))) return;
this.undelegateEvents();
for (var key in events) {
var method = this[events[key]];
var method = events[key];
if (!_.isFunction(method)) method = this[events[key]];
if (!method) throw new Error('Event "' + events[key] + '" does not exist');
var match = key.match(eventSplitter);
var eventName = match[1], selector = match[2];
Expand Down
14 changes: 14 additions & 0 deletions test/view.js
Expand Up @@ -56,6 +56,20 @@ $(document).ready(function() {
equals(counter2, 3);
});

test("View: delegateEvents allows functions for callbacks", function() {
var counter = 0;
view.el = "#qunit-banner";
var events = {"click": function() { counter++; }};
view.delegateEvents(events);
$('#qunit-banner').trigger('click');
equals(counter, 1);
$('#qunit-banner').trigger('click');
equals(counter, 2);
view.delegateEvents(events);
$('#qunit-banner').trigger('click');
equals(counter, 3);
});

test("View: undelegateEvents", function() {
var counter = counter2 = 0;
view.el = document.body;
Expand Down

0 comments on commit 1c15874

Please sign in to comment.