Skip to content

Commit

Permalink
Merge pull request #345 from michaelmior/model-find
Browse files Browse the repository at this point in the history
.find() shortcut
  • Loading branch information
PaulUithol committed Jun 19, 2013
2 parents 70ae90c + 53eb7cf commit 8d2b3f0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
16 changes: 16 additions & 0 deletions backbone-relational.js
Expand Up @@ -1662,6 +1662,22 @@
}

return model;
},

/**
* Find an instance of `this` type in 'Backbone.Relational.store'.
* - If `attributes` is a string or a number, `find` will just query the `store` and return a model if found.
* - If `attributes` is an object and is found in the store, the model will be updated with `attributes` unless `options.update` is `false`.
* @param {Object|String|Number} attributes Either a model's id, or the attributes used to create or update a model.
* @param {Object} [options]
* @param {Boolean} [options.merge=true]
* @param {Boolean} [options.parse=false]
* @return {Backbone.RelationalModel}
*/
find: function( attributes, options ) {
options || ( options = {} );
options.create = false;
return this.findOrCreate( attributes, options );
}
});
_.extend( Backbone.RelationalModel.prototype, Backbone.Semaphore );
Expand Down
9 changes: 9 additions & 0 deletions test/tests.js
Expand Up @@ -1290,6 +1290,15 @@ $(document).ready(function() {
equal( person1.get( 'name' ), 'dude' );
});

test( "constructor.find", function() {
var personColl = Backbone.Relational.store.getCollection( person1 ),
origPersonCollSize = personColl.length;

// Look for a non-existent person
person = Person.find( { id: 5001 } );
ok( !person );
});

test( "change events in relation can use changedAttributes properly", function() {
var scope = {};
Backbone.Relational.store.addModelScope( scope );
Expand Down

0 comments on commit 8d2b3f0

Please sign in to comment.