Skip to content
Browse files

Merge pull request #437 from WCPetersen/remove-extra-parse

Reuse `parsedAttributes` to remove an unnecessary parse in `findOrCreate`
  • Loading branch information...
2 parents 4b6ea23 + b7ac366 commit 825f91ce5a235a952ab36a7ea9469b6554d3b61f @PaulUithol committed
Showing with 6 additions and 5 deletions.
  1. +1 −1 backbone-relational.js
  2. +5 −4 test/tests.js
View
2 backbone-relational.js
@@ -1812,7 +1812,7 @@
model.set( parsedAttributes, options );
}
else if ( !model && options.create !== false ) {
- model = this.build( attributes, options );
+ model = this.build( parsedAttributes, _.defaults( { parse: false }, options ) );
}
}
View
9 test/tests.js
@@ -1243,9 +1243,10 @@ $(document).ready(function() {
ok( animal.get( 'livesIn' ) instanceof Zoo );
ok( animal.get( 'livesIn' ).get( 'animals' ).get( animal ) === animal );
- // `parse` gets called once by `findOrCreate` directly when trying to lookup `1`,
- // once when `build` (called from `findOrCreate`) calls the Zoo constructor with `{ parse: true}`.
- ok( parseCalled === 2, 'parse called 2 times? ' + parseCalled );
+ // `parse` gets called by `findOrCreate` directly when trying to lookup `1`,
+ // and the parsed attributes are passed to `build` (called from `findOrCreate`) with `{ parse: false }`,
+ // rather than having `parse` called again by the Zoo constructor.
+ ok( parseCalled === 1, 'parse called 1 time? ' + parseCalled );
parseCalled = 0;
@@ -2313,7 +2314,7 @@ $(document).ready(function() {
ok( job && job.id === 'e1', 'job exists' );
ok( person && person.id === 'p1', 'person exists' );
- ok( modelParseCalled === 7, 'model.parse called 7 times? ' + modelParseCalled );
+ ok( modelParseCalled === 4, 'model.parse called 4 times? ' + modelParseCalled );
ok( collParseCalled === 0, 'coll.parse called 0 times? ' + collParseCalled );
});

0 comments on commit 825f91c

Please sign in to comment.
Something went wrong with that request. Please try again.