Skip to content

Commit

Permalink
Fix _prepareModel (updates got clobbered in the merge), removed a s…
Browse files Browse the repository at this point in the history
…tray `getByCid`, fix tests.
  • Loading branch information
PaulUithol committed Dec 18, 2012
1 parent 6975378 commit bd61b20
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 42 deletions.
33 changes: 22 additions & 11 deletions backbone-relational.js
Original file line number Diff line number Diff line change
Expand Up @@ -1556,19 +1556,30 @@
*/
Backbone.Collection.prototype.__prepareModel = Backbone.Collection.prototype._prepareModel;
Backbone.Collection.prototype._prepareModel = function ( attrs, options ) {
if (attrs instanceof Backbone.Model) {
if (!attrs.collection) attrs.collection = this;
return attrs;
}
options || (options = {});
options.collection = this;
if ( typeof this.model.build !== 'undefined' ) {
var model = this.model.build( attrs, options );
var model;

if ( attrs instanceof Backbone.Model ) {
if ( !attrs.collection ) {
attrs.collection = this;
}
model = attrs;
}
else {
var model = new this.model(attrs, options);
options || (options = {});
options.collection = this;

if ( typeof this.model.findOrCreate !== 'undefined' ) {
model = this.model.findOrCreate( attrs, options );
}
else {
model = new this.model( attrs, options );
}

if ( !model._validate( attrs, options ) ) {
model = false;
}
}
if (!model._validate(attrs, options)) return false;

return model;
};

Expand Down Expand Up @@ -1626,7 +1637,7 @@

//console.debug('calling remove on coll=%o; models=%o, options=%o', this, models, options );
_.each( models || [], function( model ) {
model = this.getByCid( model ) || this.get( model );
model = this.get( model );

if ( model instanceof Backbone.Model ) {
remove.call( this, model, options );
Expand Down
55 changes: 24 additions & 31 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,6 @@ $(document).ready(function() {
});

test( "getObjectByName", function() {
equal( Backbone.Relational.store.getObjectByName( 'Backbone' ), Backbone );
equal( Backbone.Relational.store.getObjectByName( 'Backbone.RelationalModel' ), Backbone.RelationalModel );
});

Expand Down Expand Up @@ -2167,49 +2166,43 @@ $(document).ready(function() {
// Property.setup()

var Property, View,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };

View = (function(_super) {
View = ( function( _super ) {
__extends(View, _super);

__extends(View, _super);
View.name = 'View';

View.name = 'View';

function View() {
return View.__super__.constructor.apply(this, arguments);
}

return View;
function View() {
return View.__super__.constructor.apply( this, arguments );
}

})(Backbone.RelationalModel);
return View;
})( Backbone.RelationalModel );

View.setup();

Property = (function(_super) {
__extends(Property, _super);

__extends(Property, _super);
Property.name = 'Property';

Property.name = 'Property';

function Property() {
return Property.__super__.constructor.apply(this, arguments);
}

Property.prototype.relations = [
{
type: Backbone.HasOne,
key: 'view',
relatedModel: View,
reverseRelation: {
type: Backbone.HasMany,
key: 'properties'
}
function Property() {
return Property.__super__.constructor.apply(this, arguments);
}
];

return Property;
Property.prototype.relations = [{
type: Backbone.HasOne,
key: 'view',
relatedModel: View,
reverseRelation: {
type: Backbone.HasMany,
key: 'properties'
}
}];

return Property;
})(Backbone.RelationalModel);

Property.setup();
Expand Down

0 comments on commit bd61b20

Please sign in to comment.