defaulting Backbone.Collection#model to be Backbone.Model

This commit is contained in:
Jeremy Ashkenas
2010-10-06 13:41:37 -04:00
parent 80acf9484d
commit b1277b9258
2 changed files with 13 additions and 2 deletions

View File

@@ -276,7 +276,8 @@
// or unordered. If a `comparator` is specified, the Collection will maintain
// its models in sort order, as they're added and removed.
Backbone.Collection = function(models, options) {
if (options && options.comparator) {
options || (options = {});
if (options.comparator) {
this.comparator = options.comparator;
delete options.comparator;
}
@@ -288,9 +289,11 @@
// Define the Collection's inheritable methods.
_.extend(Backbone.Collection.prototype, Backbone.Bindable, {
model : Backbone.Model,
// Initialize or re-initialize all internal state. Called when the
// collection is refreshed.
_initialize : function() {
_initialize : function(options) {
this.length = 0;
this.models = [];
this._byId = {};

View File

@@ -79,4 +79,12 @@ $(document).ready(function() {
equals(lastRequest[1], col);
});
test("collections: create", function() {
var model = col.create({label: 'f'});
equals(lastRequest[0], 'POST');
equals(lastRequest[1], model);
equals(model.get('label'), 'f');
equals(model.collection, col);
});
});