Skip to content

Commit

Permalink
Clear event callbacks with clearEvents
Browse files Browse the repository at this point in the history
  • Loading branch information
leshill committed Sep 19, 2011
1 parent c62e613 commit 1aaffa1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
7 changes: 6 additions & 1 deletion backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@
delegateEvents : function(events) {
if (!(events || (events = this.events))) return;
if (_.isFunction(events)) events = events.call(this);
$(this.el).unbind('.delegateEvents' + this.cid);
this.clearEvents();
for (var key in events) {
var method = this[events[key]];
if (!method) throw new Error('Event "' + events[key] + '" does not exist');
Expand All @@ -970,6 +970,11 @@
}
},

// Clears all callbacks previously bound to the view with `delegateEvents`.
clearEvents: function() {
$(this.el).unbind('.delegateEvents' + this.cid);
},

// Performs the initial configuration of a View with a set of options.
// Keys with special meaning *(model, collection, id, className)*, are
// attached directly to the view.
Expand Down
21 changes: 21 additions & 0 deletions test/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ $(document).ready(function() {
equals(counter2, 3);
});

test("View: clearEvents", function() {
var counter = counter2 = 0;
view.el = document.body;
view.increment = function(){ counter++; };
$(view.el).unbind('click');
$(view.el).bind('click', function(){ counter2++; });
var events = {"click #qunit-userAgent": "increment"};
view.delegateEvents(events);
$('#qunit-userAgent').trigger('click');
equals(counter, 1);
equals(counter2, 1);
view.clearEvents();
$('#qunit-userAgent').trigger('click');
equals(counter, 1);
equals(counter2, 2);
view.delegateEvents(events);
$('#qunit-userAgent').trigger('click');
equals(counter, 2);
equals(counter2, 3);
});

test("View: _ensureElement with DOM node el", function() {
var ViewClass = Backbone.View.extend({
el: document.body
Expand Down

0 comments on commit 1aaffa1

Please sign in to comment.