javascript - backbone.js 上的无限滚动/分页不起作用

标签 javascript jquery ajax backbone.js underscore.js

我正在尝试使用 Pagination plugin让 backbone.js 创建无限滚动功能,就像在 Twitter 中一样,单击按钮/链接 #pagination a 将从后端加载下一页结果并将其附加到当前 View 照片 ListView

问题:我正在尝试遵循插件的示例 here和来源here .到目前为止,我设法通过单击链接 #pagination a 从后端获取 JSON 数据 data

当按钮 #pagination a View 中的 PaginationView 是否被点击?

我以为paginationView中的appendRender方法的this.collection.requestNextPage()被调用时,会添加新获取的模型到集合 photoCollection,它将触发 photoListViewthis.collection.bind('change', this.render, this); 事件触发器,导致新检索到的模型被附加?

观看次数

PhotoListView = Backbone.View.extend({
    el: '#photo_list',

    initialize: function() {
        this.collection.bind('change', this.render, this);
        this.collection.bind('add', function() {
            console.log('added to collection');
        }, this);
    },

    render: function() {
        //$(this.el).html('');
        this.collection.each(function(photo, index) {
            if(index % 7 < 3) {
                $(this.el).append(new PhotoListItemView({ model: photo }).render().el);
            } else {
                $(this.el).append(new PhotoListQuadItemView({ model: photo }).render().el);
            }
            console.log('render');
        }, this);
        return this;
    }
});

PhotoListItemView = Backbone.View.extend({
    tagNAme: 'div',
    className: 'photo_box',

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

    initialize: function() {
        this.model.bind('destroy', this.close, this);
    },

    render: function() {
        $(this.el).html( this.template( this.model.toJSON() ) );
            console.log('render item');
        return this;
    },

    close: function() {
        this.unbind();
        this.remove();
    }
});

PhotoListQuadItemView = Backbone.View.extend({
    tagNAme: 'div',
    className: 'photo_box',

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

    initialize: function() {
        this.model.bind('destroy', this.close, this);
    },

    render: function() {
        $(this.el).html( this.template( this.model.toJSON() ) );
        return this;
    },

    close: function() {
        this.unbind();
        this.remove();
    }
});

PaginationView = Backbone.View.extend({
    el: $('#pagination'),

    events: {
        'click #pagination a': 'appendRender'
    },

    initialize: function() {
        this.render();
    },

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

    render: function() {
        $(this.el).html( this.template() );
    },

    appendRender: function() {
        this.collection.requestNextPage()
            .done(function(data, textStatus, jqXHR) {
                // HOW DO I APPEND THE NEW DATA VIEWS?
        });
    }
});

收藏

PhotoCollection = Backbone.Paginator.requestPager.extend({
    model: Photo,

    paginator_core: {
            dataType: 'json',
            url: 'http://www.domain.com/explore/all?'
        },

    paginator_ui: {
        firstPage: 1,
        currentPage: 1,
        perPage: 7,
        totalPages: 10
    },

    server_api: {
        '$page': function() { return this.currentPage; }
    },

    parse: function (response) {
        return response;
    }

});

路由器

var AppRouter = Backbone.Router.extend({
    routes: {
        '': 'explore'
    },

    initialize: function() {

    },

    explore: function() {
        console.log('explore!');
        this.photoList = new PhotoCollection();
        this.paginationView = new PaginationView({ collection: this.photoList });
        var self = this;
        this.photoList.fetch({
            success: function() {
                self.photoListView = new PhotoListView({ collection: self.photoList });
                self.photoListView.render();

            }
        });
    }

});

var app = new AppRouter();
Backbone.history.start();

最佳答案

正如 Kishore 所说。绑定(bind)到 reset 事件,现在可以使用了。

关于javascript - backbone.js 上的无限滚动/分页不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11353454/

相关文章:

c# - 将大型阵列传输到客户端

javascript - 将 freemarker 与 javascript ES6 模板字符串一起使用

javascript - 如何使用 React 和 Office Fabric 在详细信息列表中聚焦按钮

javascript - 为什么参数读取为true?

javascript - jQuery AJAX 问题,间歇性地获取重复请求

javascript - 响应式导航菜单,元素 "tuck under"彼此

javascript - 如何使用本地存储进行事件类?

Visual Studio Code 中的 JQuery 智能感知

php - 如何使用 PHP 和 AJAX 过滤服装尺寸?

javascript - Yii Ajax 在 LimeSurvey 插件环境中返回整个网页。为什么?