Permalink
Browse files

Also delete `options.collection` on set in `findOrCreate`. Fixes #315

  • Loading branch information...
1 parent 51c6f89 commit 0bd577ef74e7a76edbb06e9895fb3aac4a3c82f5 @PaulUithol committed Apr 1, 2013
Showing with 8 additions and 7 deletions.
  1. +5 −2 backbone-relational.js
  2. +3 −5 test/tests.js
View
@@ -1077,7 +1077,7 @@
var dit = this,
collection = this.collection = options.collection;
- // Prevent this option from cascading down to related models; they shouldn't go into this `if` clause.
+ // Prevent `collection` from cascading down to nested models; they shouldn't go into this `if` clause.
delete options.collection;
this._deferProcessing = true;
@@ -1647,6 +1647,9 @@
// If not, create an instance (unless 'options.create' is false).
if ( _.isObject( attributes ) ) {
if ( model && options.merge !== false ) {
+ // Make sure `options.collection` doesn't cascade to nested models
+ delete options.collection;
+
model.set( parsedAttributes, options );
}
else if ( !model && options.create !== false ) {
@@ -1676,7 +1679,7 @@
model = attrs;
}
else {
- options || (options = {});
+ options || ( options = {} );
options.collection = this;
if ( typeof this.model.findOrCreate !== 'undefined' ) {
View
@@ -3413,7 +3413,7 @@ $(document).ready(function() {
ok( grandchild.get( 'parent' ), 'reverse relation from `grandchild` to `child` works');
- ok( grandchild.get( 'children' ).first() === parent, 'reverse relation from `grandchild` to `child` works');
+ ok( grandchild.get( 'children' ).first() === parent, 'reverse relation from `grandchild` to `parent` works');
ok( parent.get( 'parent' ) === grandchild, 'circular reference from `grandchild` to `parent` works' );
});
@@ -3439,9 +3439,7 @@ $(document).ready(function() {
]);
var parent = nodes.first();
- ok( parent, 'first item accessible after resetting collection' );
-
- ok( parent.collection === nodes, '`parent.collection` is set to `nodes`' );
+ ok( parent && parent.id === 1, 'first item accessible after resetting collection' );
var child = parent.get( 'children' ).first();
ok( child, '`child` can be retrieved from `parent`' );
@@ -3452,7 +3450,7 @@ $(document).ready(function() {
ok( grandchild.get( 'parent' ), 'reverse relation from `grandchild` to `child` works');
- ok( grandchild.get( 'children' ).first() === parent, 'reverse relation from `grandchild` to `child` works');
+ ok( grandchild.get( 'children' ).first() === parent, 'reverse relation from `grandchild` to `parent` works');
ok( parent.get( 'parent' ) === grandchild, 'circular reference from `grandchild` to `parent` works' );
});

0 comments on commit 0bd577e

Please sign in to comment.