Saturday, 17 August 2013

Backbone.js: Collection filter is not working properly

Backbone.js: Collection filter is not working properly

I have a filter method for a Backbonejs collection that does not work. Why
isn't this properly filtering out the right items?
var Notes = Backbone.Collection.extend({
model: Note,
url: '/notes',
comparator: function(note) {
return note.get('timecode');
},
sortByField: function() {
this.sort();
},
filter_vimeoId: function(vimeo_id) {
filtered = this.filter(function(note) {
return note.get('vimeo_id') === vimeo_id;
});
return new Notes(filtered);
}
});
// Notes
var notes = new Notes([
{text: 'foo', timecode: 5, vimeo_id: 123},
{text: 'bar', timecode: 15, vimeo_id: 789},
{text: 'baz', timecode: 25, vimeo_id: 789}
]);
// Filtering...
vimeo_id = 123;
var filteredNotes = notes.filter_vimeoId(vimeo_id);
// `filteredNotes` returns all notes, not just the note with the 123 vimeoid

No comments:

Post a Comment