Skip to content

Commit

Permalink
Merge branch 'master' into view-native-hooks
Browse files Browse the repository at this point in the history
Conflicts:
	backbone.js
  • Loading branch information
akre54 committed Mar 14, 2014
2 parents 264bd9b + b5f1574 commit c5c0d2e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
14 changes: 7 additions & 7 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@

// Create local references to array methods we'll want to use later.
var array = [];
var push = array.push;
var slice = array.slice;
var splice = array.splice;

// Current version of the library. Keep in sync with `package.json`.
Backbone.VERSION = '1.1.2';
Expand Down Expand Up @@ -703,15 +701,18 @@
var toAdd = [], toRemove = [], modelMap = {};
var add = options.add, merge = options.merge, remove = options.remove;
var order = !sortable && add && remove ? [] : false;
var targetProto = this.model.prototype;

// Turn bare objects into model references, and prevent invalid models
// from being added.
for (var i = 0, length = models.length; i < length; i++) {
attrs = models[i] || {};
if (attrs instanceof Model) {
id = model = attrs;
} else if (targetProto.generateId) {
id = targetProto.generateId(attrs);
} else {
id = this.model.prototype.generateId(attrs);
id = attrs[targetProto.idAttribute || Model.prototype.idAttribute];
}

// If a duplicate is found, prevent it from being added and
Expand Down Expand Up @@ -1030,7 +1031,6 @@
_.extend(this, _.pick(options, viewOptions));
this._ensureElement();
this.initialize.apply(this, arguments);
this.delegateEvents();
};

// Cached regex to split keys for `delegate`.
Expand Down Expand Up @@ -1080,10 +1080,10 @@

// Change the view's element (`this.el` property) and re-delegate the
// view's events on the new element.
setElement: function(element, attributes, delegate) {
setElement: function(element, attributes) {
this.undelegateEvents();
this._setElement(element, attributes);
if (delegate !== false) this.delegateEvents();
this.delegateEvents();
return this;
},

Expand Down Expand Up @@ -1161,7 +1161,7 @@
var attrs = _.extend({}, _.result(this, 'attributes'));
if (this.id) attrs.id = _.result(this, 'id');
if (this.className) attrs['class'] = _.result(this, 'className');
this.setElement(document.createElement(_.result(this, 'tagName')), attrs, false);
this.setElement(document.createElement(_.result(this, 'tagName')), attrs);
} else {
this.setElement(_.result(this, 'el'), null, false);
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"underscore" : ">=1.5.0"
},
"devDependencies": {
"phantomjs": "1.9.0-1",
"docco": "0.6.1",
"coffee-script": "1.6.1"
"phantomjs": "1.9.7-1",
"docco": "0.6.3",
"coffee-script": "1.7.1"
},
"scripts": {
"test": "phantomjs test/vendor/runner.js test/index.html?noglobals=true && coffee test/model.coffee",
Expand Down
54 changes: 54 additions & 0 deletions test/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -1362,4 +1362,58 @@
equal(collection.get(1), collection.first());
});

test('Polymorphic models work with "simple" constructors', function () {
var A = Backbone.Model.extend();
var B = Backbone.Model.extend();
var C = Backbone.Collection.extend({
model: function (attrs) {
return attrs.type === 'a' ? new A(attrs) : new B(attrs);
}
});
var collection = new C([{id: 1, type: 'a'}, {id: 2, type: 'b'}]);
equal(collection.length, 2);
ok(collection.at(0) instanceof A);
equal(collection.at(0).id, 1);
ok(collection.at(1) instanceof B);
equal(collection.at(1).id, 2);
});

test('Polymorphic models work with "advanced" constructors', function () {
var A = Backbone.Model.extend({idAttribute: '_id'});
var B = Backbone.Model.extend({idAttribute: '_id'});
var C = Backbone.Collection.extend({
model: Backbone.Model.extend({
constructor: function (attrs) {
return attrs.type === 'a' ? new A(attrs) : new B(attrs);
},

idAttribute: '_id'
})
});
var collection = new C([{_id: 1, type: 'a'}, {_id: 2, type: 'b'}]);
equal(collection.length, 2);
ok(collection.at(0) instanceof A);
equal(collection.at(0).id, 1);
ok(collection.at(1) instanceof B);
equal(collection.at(1).id, 2);

A.prototype.generateId = B.prototype.generateId = function (attrs) {
return attrs.type + '-' + attrs.id;
}
C = Backbone.Collection.extend({
model: Backbone.Model.extend({
constructor: function (attrs) {
return attrs.type === 'a' ? new A(attrs) : new B(attrs);
},

generateId: A.prototype.generateId
})
});
collection = new C([{id: 1, type: 'a'}, {id: 1, type: 'b'}]);
equal(collection.length, 2);
ok(collection.at(0) instanceof A);
equal(collection.at(0).id, 'a-1');
ok(collection.at(1) instanceof B);
equal(collection.at(1).id, 'b-1');
});
})();

0 comments on commit c5c0d2e

Please sign in to comment.