Merge branch 'multiple-views-per-element' of https://github.com/sstephenson/backbone

This commit is contained in:
Jeremy Ashkenas
2010-12-13 09:34:58 -05:00
2 changed files with 26 additions and 2 deletions

View File

@@ -772,6 +772,7 @@
// Creating a Backbone.View creates its initial element outside of the DOM,
// if an existing element is not provided...
Backbone.View = function(options) {
this.vid = _.uniqueId('v');
this._configure(options || {});
this._ensureElement();
this.delegateEvents();
@@ -843,13 +844,13 @@
// not `change`, `submit`, and `reset` in Internet Explorer.
delegateEvents : function(events) {
if (!(events || (events = this.events))) return;
$(this.el).unbind('.delegateEvents');
$(this.el).unbind('.delegateEvents' + this.vid);
for (var key in events) {
var methodName = events[key];
var match = key.match(eventSplitter);
var eventName = match[1], selector = match[2];
var method = _.bind(this[methodName], this);
eventName += '.delegateEvents';
eventName += '.delegateEvents' + this.vid;
if (selector === '') {
$(this.el).bind(eventName, method);
} else {

View File

@@ -64,4 +64,27 @@ $(document).ready(function() {
equals(view.el, document.body);
});
test("View: multiple views per element", function() {
var count = 0, ViewClass = Backbone.View.extend({
el: $("body"),
events: {
"click": "click"
},
click: function() {
count++;
}
});
var view1 = new ViewClass;
$("body").trigger("click");
equals(1, count);
var view2 = new ViewClass;
$("body").trigger("click");
equals(3, count);
view1.delegateEvents();
$("body").trigger("click");
equals(5, count);
});
});