javascript - Backbone search collection for model that has attribute that matches string -
i writing search function @ moment backbone application, idea user can enter string , app search , return matching models string appears in of attributes. far have following,
view function run on keyup,
this.results = this.collection.search(letters);
this runs following code located in collection,
search: function( filtervalue ) { filtervalue = filtervalue.tolowercase(); var filterthroughvalue = function(data) { return _.some(_.values(data.tojson()), function(value) { console.log(value); if(value != undefined) { value = (!isnan(value) ? value.tostring() : value); return value.tolowercase().indexof(filtervalue) >= 0; } }); }; return app.collections.filtercollection = this.filter(filterthroughvalue); }
however running following error,
uncaught typeerror: undefined not function
this error shown being line return value.tolowercase().indexof(filtervalue) >= 0;
, error gets returned whether or not use string know exists in model of collection.
is there fix this, or better way of searching , returning models have models contain search string?
since want string representation of value, can check if value has tostring method. note string has tostring method, work if value string.
return _.some(_.values(data.tojson()), function(value) { if(value && value.tostring) { return value.tostring().tolowercase().indexof(filtervalue) >= 0; } return false; }
Comments
Post a Comment