Skip to content

Commit

Permalink
Merge pull request #19 from braddunbar/view-hooks
Browse files Browse the repository at this point in the history
A little clarification.
  • Loading branch information
akre54 committed Mar 14, 2014
2 parents b79bd78 + 72f7da4 commit 155a738
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 57 deletions.
8 changes: 3 additions & 5 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -1065,15 +1065,15 @@
// Remove this view by taking the element out of the DOM, and removing any
// applicable Backbone.Events listeners.
remove: function() {
this._remove();
this._removeElement();
this.stopListening();
return this;
},

// Remove this view's element from the document and all event listeners
// attached to it. Exposed for subclasses using an alternative DOM
// manipulation API.
_remove: function() {
_removeElement: function() {
this.$el.remove();
},

Expand Down Expand Up @@ -1117,10 +1117,8 @@
var method = events[key];
if (!_.isFunction(method)) method = this[events[key]];
if (!method) continue;

var match = key.match(delegateEventSplitter);
var eventName = match[1], selector = match[2];
this.delegate(eventName, selector, _.bind(method, this));
this.delegate(match[1], match[2], _.bind(method, this));
}
return this;
},
Expand Down
76 changes: 24 additions & 52 deletions test/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,13 @@
ok(result.length === +result.length);
});

test("jQuery", function() {
test("$el", 3, function() {
var view = new Backbone.View;
view.setElement('<p><a><b>test</b></a></p>');

strictEqual(view.el.nodeType, 1);

if (Backbone.$) {
ok(view.$el instanceof Backbone.$);
strictEqual(view.$el[0], view.el);
} else {
strictEqual(view.$el, undefined);
}
ok(view.$el instanceof Backbone.$);
strictEqual(view.$el[0], view.el);
});

test("initialize", 1, function() {
Expand Down Expand Up @@ -78,18 +73,14 @@
});

test("delegate", 2, function() {
var counter1 = 0, counter2 = 0;

var view = new Backbone.View({el: '#testElement'});
view.increment = function(){ counter1++; };
view.$el.on('click', function(){ counter2++; });

view.delegate('click', 'h1', view.increment);
view.delegate('click', view.increment);

view.delegate('click', 'h1', function() {
ok(true);
});
view.delegate('click', function() {
ok(true);
});
view.$('h1').trigger('click');
equal(counter1, 2);
equal(counter2, 1);
});

test("delegateEvents allows functions for callbacks", 3, function() {
Expand Down Expand Up @@ -167,33 +158,22 @@
});

test("undelegate with selector", 2, function() {
var counter1 = 0, counter2 = 0;
view = new Backbone.View({el: '#testElement'});
view.delegate('click', function() { counter1++; });
view.delegate('click', 'h1', function() { counter2++; });

view.delegate('click', function() { ok(true); });
view.delegate('click', 'h1', function() { ok(false); });
view.undelegate('click', 'h1');

view.$('h1').trigger('click');
view.$el.trigger('click');

equal(counter1, 2);
equal(counter2, 0);
});

test("undelegate with handler and selector", 2, function() {
var counter1 = 0, counter2 = 0;
view = new Backbone.View({el: '#testElement'});
view.delegate('click', function() { counter1++; });
var handler = view.delegate('click', 'h1', function() { counter2++; });

view.delegate('click', function() { ok(true); });
var handler = function(){ ok(false); };
view.delegate('click', 'h1', handler);
view.undelegate('click', 'h1', handler);

view.$('h1').trigger('click');
view.$el.trigger('click');

equal(counter1, 2);
equal(counter2, 0);
});

test("_ensureElement with DOM node el", 1, function() {
Expand Down Expand Up @@ -284,25 +264,18 @@
});

test("custom events", 2, function() {
var count = 0;

var View = Backbone.View.extend({
el: $('body'),
events: function() {
return {"fake$event": "run"};
},
run: function() {
count++;
events: {
"fake$event": function() { ok(true); }
}
});

var view = new View;
$('body').trigger('fake$event').trigger('fake$event');
equal(count, 2);

$('body').off('fake$event');
$('body').trigger('fake$event');
equal(count, 2);
});

test("#1048 - setElement uses provided object.", 2, function() {
Expand Down Expand Up @@ -353,7 +326,7 @@
}
});

ok(new View().el.tagName.toLowerCase() == 'p');
ok(new View().$el.is('p'));
});

test("views stopListening", 0, function() {
Expand Down Expand Up @@ -382,8 +355,8 @@
});

var view = new View;
ok(view.el.tagName.toLowerCase() == 'p');
ok(view.$('a').length != 0);
ok(view.$el.is('p'));
ok(view.$el.has('a'));
});

test("events passed in options", 1, function() {
Expand All @@ -406,20 +379,19 @@
equal(counter, 2);
});

test("remove", 2, function() {
var el = view.el;
test("remove", 1, function() {
var view = new Backbone.View;
document.body.appendChild(view.el);

view.delegate('click', function() { ok(false); });
view.listenTo(view, 'all x', function() { ok(false); });

view.remove();

strictEqual(view.el, el);
notEqual(view.el.parentNode, document.body);

view.$el.trigger('click');
view.trigger('x');

// In IE8 and below, parentNode still exists but is not document.body.
notEqual(view.el.parentNode, document.body);
});

})();

0 comments on commit 155a738

Please sign in to comment.