What the Underscore.where
method returns is Array
not a Backbone.Collection
, so it did not define a toJSON
method.
So there are two things you can do:
Iterate over the elements and display the result:
var result = friends.where({job: "Musketeer"}); _.map( result, function( model ){ return model.toJSON(); } );
jsFiddle code
Implement a collection search method that returns the correct Backbone.Collection:
var Friends = Backbone.Collection.extend({ search: function( opts ){ var result = this.where( opts ); var resultCollection = new Friends( result ); return resultCollection; } }); var myFriends = new Friends([ {name: "Athos", job: "Musketeer"}, {name: "Porthos", job: "Musketeer"}, {name: "Aramis", job: "Musketeer"}, {name: "d'Artagnan", job: "Guard"}, ]); myFriends.search({ job: "Musketeer" }).toJSON();
jsFiddle code
source share