【问题标题】:How to sort items in backbone.js如何对backbone.js中的项目进行排序
【发布时间】:2014-03-10 17:43:09
【问题描述】:

我正在学习主干.js,所以我决定使用主干.js 和本地存储插件制作我自己的 Todo 应用程序。我已经有了 Todo 应用程序,您可以在其中添加和删除待办事项,现在我正在努力使它们在已完成和未完成的任务中可排序。但问题是我找不到办法。我在视图中创建了一个名为 sort_done() 的方法

    sort_done: function(){
    var done = this.collection.where({done: true});
    console.log(done);
}

但我不知道如何查看视图以显示已完成或未完成的任务,如果有人能给我一些关于如何管理此类“问题”的建议,我将不胜感激。模型、集合和视图js我也留给大家看看。

型号:

    var Task = Backbone.Model.extend({
    defaults:{
        title: "An empyt task..",
        done: false
    },

    validate: function(attrs){
        if(! $.trim(attrs.title)){
            return "The task has no title"
        }
    }
});

var task = new Task;

收藏:

var Tasks = Backbone.Collection.extend({
model: Task,

localStorage: new Backbone.LocalStorage("todos-collection")
});

var tasks = new Tasks();

观看次数:

   var TaskView = Backbone.View.extend({
    tagName: "li",

    template: _.template( $('#task').html() ),

    initialize: function(){
        this.model.on('change', this.render, this);
        this.model.on('destroy', this.remove, this);
    },

    render: function(){
        var template = this.template( this.model.toJSON() );

        this.$el.html( template );

        return this;
    },

    events: {
        'click .icon-checkbox': 'toggleState',
        'click .task_title': 'editTask',
        'keypress .edit': 'updateOnEnter',
        'click  .close_btn': 'clear'
    },

    toggleState: function(e){
        var $checkbox = $(e.target);
        this.model.save('done', !this.model.get('done'));

    },

    editTask: function(e){
        this.task = $(e.target);
        this.editBox = this.task.next();
        this.editInput = this.editBox.find('.edit');

        $(".task_title").removeClass("display__none");
        $(".editBox").removeClass("edit_box__editing");
        this.task.addClass("display__none")
        this.editBox.addClass("edit_box__editing");
        this.editInput.attr('placeholder', this.task.text()).focus();
    },

    updateOnEnter: function(e){
        if(e.keyCode === 13){
            this.close();
        }
    },

    close: function(){
        var value = this.editInput.val();
        if(!value){
            this.task.removeClass("display__none")
            this.editBox.removeClass("edit_box__editing");
        }else{
            this.model.save({title: value});
            this.task.removeClass("display__none")
            this.editBox.removeClass("edit_box__editing");
        }
    },

    clear:function(){
        this.model.destroy();
    }
});

var TasksView = Backbone.View.extend({
    el: '#tasks',

    initialize: function(){
        this.render();
        this.collection.on('add', this.addOne, this);
        this.collection.on()
    },

    render: function(){
        this.collection.each(this.addOne, this);

        return this;

    },

    addOne: function(task){
        var taskView = new TaskView({ model: task });
        this.$el.append( taskView.render().el );
    }
});

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 the input

        if($.trim(taskTitle) === ''){//check if the input has some text in it
            this.displayMessage("Todo's can not be empty");
        }else{
            var task = new Task( {title: taskTitle} ); // create the task model
            this.collection.create(task); //add the model to the 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});
        console.log(done);
    }

});


var tasksView = new TasksView( {collection: tasks} );
var addTask = new AddTask( {collection: tasks} );

非常感谢!

【问题讨论】:

  • Backbone.js 依赖于 Underscore.js。您可以使用_.sortBy 方法。
  • 是的,但实际上我的问题是使用下划线的 _.sortBy 方法或 this.collection.where({done: true});,我不知道如何使用返回对象

标签: javascript jquery sorting backbone.js underscore.js


【解决方案1】:

当您调用collection.where 时,您的集合没有被过滤,它只是返回过滤后的模型(不更改初始集合),所以对于您的问题,您必须这样做:

initialize: function(){
    this.render();
    this.collection.on('add', this.addOne, this);
    this.collection.on('reset', this.render, this); // here I change the event to reset
},

...

sort_done: function(){
    var done = this.collection.where({done: true});
    this.collection.reset(done);
    console.log(done);
}

【讨论】:

  • 但是如果我触发排序,我将按原样渲染视图,因此它将按原样渲染,因为我没有传递渲染的集合或类似的东西
  • 不,您的收藏已经链接到您的视图var tasksView = new TasksView( {collection: tasks} );,所以在tasksView 渲染函数中,您可以通过调用this.collection 来获取收藏。
  • 我已经尝试过了,但是我渲染了相同的视图,没有排序,而且我的视图在 previwsly 渲染之后渲染,所以我得到了重复的内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 2013-06-08
  • 1970-01-01
  • 2014-07-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多