javascript - Sort by attributes Backbone.js -
i trying sort collection , update view sorted collection. trying sort todo list done or not done tasks. in collection view have method:
var addtask = backbone.view.extend({ el: '#todos', initialize: function(){ this.collection.fetch(); }, events: { 'click #add': 'addtask', 'click #filter_done': 'sort_done', 'keypress #inputtask': 'updateonenter' }, addtask: function(){ var tasktitle = $('#inputtask'). val(); $('#inputtask').val(""); //clear input if($.trim(tasktitle) === ''){//check if input has text in this.displaymessage("todo's can not empty"); }else{ var task = new task( {title: tasktitle} ); // create task model this.collection.create(task); //add model collection } }, displaymessage: function(msg){ $('#inputtask').focus().attr("placeholder", msg); }, updateonenter: function(e){ if(e.keycode === 13){ this.addtask(); } }, sort_done: function(){ var done = this.collection.where({done: true}); } }); var addtask = new addtask( {collection: tasks} ); my problem don't know how ask view render value returned sort_done method, , don't want lose information contained in original collection.
one way set comparator on collection, , re-render view on collection's 'sort' event.
initialize: function() { // ... this.collection.on('sort', this.render, this); }, sort_done: function() { this.collection.comparator = function(model) { return model.get('done') ? 1 : -1; }; // trigger 'sort' event on collection this.collection.sort(); } one disadvantage approach if collection shared between views, affect views of collection.
Comments
Post a Comment