使用Backbone.js在待办事项中对数据集合进行分页.js Web应用程序



我正在研究这个网络应用程序,todos.js,它在这个网址上有很好的记录。

我想添加一个选项,以便每页显示有限数量的项目。

这是我的尝试,它有效,但我不确定这是否是完成此任务的正确方法:

var AppView = Backbone.View.extend({
    firstPage: 0,
    perPage: 2,
    counter: 0,
......
    addOne: function addOne (todo) 
    {
        var view,
            isIntoRange;
        view = new TodoView({
            model: todo
        });
        isIntoRange = (
            this.counter >= (this.firstPage * this.perPage) 
            &&
            this.counter < (this.firstPage * this.perPage) + this.perPage   
        );
        if (isIntoRange) {             
            this.$("#todo-list").append(view.render().el);
        }
        this.counter += 1;
    },
    addAll: function() {
        Todos.each(this.addOne);
    },
  .....
});

说了主干分页器是一个很好的选择,我不会修改执行此任务的方法addOne

相反,我将添加一个可以接受两个参数的新方法showTasksfirstPageperPage

    showTasks: function showTasks (firstPage, perPage)
    {
        // your code
    }

然后,由于此方法应从链接调用,因此我将使用 backbone.router 来呈现页面:

   var AppRouter = Backbone.Router.extend({
       ....
       linkAction: function(firstPage, perPage){
           taskView.showTasks(firstPage, perPage);
       },
       ....
   });

最新更新