mirror of
https://github.com/jashkenas/backbone.git
synced 2026-04-30 03:00:06 -04:00
add an undelegate method to View and pass back the listener from delegate
This commit is contained in:
19
backbone.js
19
backbone.js
@@ -1129,17 +1129,19 @@
|
||||
|
||||
// Add a single event listener to the element responding only to the
|
||||
// optional `selector` or catches all `eventName` events. Subclasses can
|
||||
// override this to utilize an alternative DOM event management API.
|
||||
// override this to utilize an alternative DOM event management API. Returns
|
||||
// the listener for easy undelegation with `undelegate`.
|
||||
delegate: function(eventName, selector, listener) {
|
||||
eventName += '.delegateEvents' + this.cid;
|
||||
if (!selector) {
|
||||
// When `delegate` is called with two arguments, `selector` is actually
|
||||
// the `listener`
|
||||
var listener = _.isFunction(selector) ? selector : listener;
|
||||
if (selector === '') {
|
||||
this.$el.on(eventName, listener);
|
||||
} else {
|
||||
// When `delegate` is called with two arguments, `selector` is actually
|
||||
// the `listener`
|
||||
this.$el.on(eventName, selector, listener);
|
||||
}
|
||||
return this;
|
||||
return listener;
|
||||
},
|
||||
|
||||
// Clears all callbacks previously bound to the view by `delegateEvents`.
|
||||
@@ -1150,6 +1152,13 @@
|
||||
return this;
|
||||
},
|
||||
|
||||
// Remove a single event from the delegated events. `selector` and `listener`
|
||||
// are both optional.
|
||||
undelegate: function(eventName, selector, listener) {
|
||||
eventName += '.delegateEvents' + this.cid;
|
||||
this.$el.off(eventName, selector, listener);
|
||||
},
|
||||
|
||||
// Ensure that the View has a DOM element to render into.
|
||||
// If `this.el` is a string, pass it through `$()`, take the first
|
||||
// matching element, and re-assign it to `el`. Otherwise, create
|
||||
|
||||
31
test/view.js
31
test/view.js
@@ -144,6 +144,37 @@
|
||||
equal(counter2, 3);
|
||||
});
|
||||
|
||||
test("undelegate", 0, function() {
|
||||
view.delegate('click', function() { ok(false); });
|
||||
view.delegate('click', 'h1', function() { ok(false); });
|
||||
|
||||
view.undelegate('click');
|
||||
|
||||
view.$('h1').trigger('click');
|
||||
view.$el.trigger('click');
|
||||
})
|
||||
|
||||
test("undelegate with passed handler", 1, function() {
|
||||
var listener = view.delegate('click', function() { ok(false); });
|
||||
view.delegate('click', function() { ok(true); });
|
||||
view.undelegate('click', listener);
|
||||
view.$el.trigger('click');
|
||||
});
|
||||
|
||||
test("undelegate with selector", 2, function() {
|
||||
var counter1 = 0, counter2 = 0;
|
||||
view.delegate('click', function() { counter1++; });
|
||||
view.delegate('click', 'h1', function() { counter2++; });
|
||||
|
||||
view.undelegate('click', 'h1');
|
||||
|
||||
view.$('h1').trigger('click');
|
||||
view.$el.trigger('click');
|
||||
|
||||
equal(counter1, 1);
|
||||
equal(counter2, 0);
|
||||
});
|
||||
|
||||
test("_ensureElement with DOM node el", 1, function() {
|
||||
var View = Backbone.View.extend({
|
||||
el: document.body
|
||||
|
||||
Reference in New Issue
Block a user