backbone.js无效滚动/分页无法正常工作

时间:2012-07-05 22:36:11

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

我正在尝试使用Pagination plugin for backbone.js来创建无限滚动功能,就像在Twitter中一样,点击按钮/链接#pagination a将从后端加载下一页结果将其附加到当前视图PhotoListView

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

如何在视图{{1}中的按钮photoListItemView时,使用backbone.js的模型视图绑定将新视图photoListView附加到视图#pagination a点击了吗?

我认为当调用PaginationViewthis.collection.requestNextPage()方法的appendRender时,新检索的模型将被添加到集合paginationView中,这将触发photoCollection } photoListView事件触发器,导致新检索的模型被追加?

视图

this.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;
    }

});

1 个答案:

答案 0 :(得分:0)

正如基肖尔所说的那样。绑定到reset事件,现在可以使用。