首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将集合传递给多个视图

将集合传递给多个视图
EN

Stack Overflow用户
提问于 2012-08-20 09:56:22
回答 1查看 146关注 0票数 0

我试图将一个分页集合(backbone.paginator插件)传递给多个视图,这样就可以共享它,并且可以使用相同的集合调用三个视图的函数。

但是,我的主视图(AttendeesView)正在调用其他视图(每页调用PaginationBarViewAttendeeView )。我试着通过视图的构造函数和路由器传递它。我似乎无法使它工作,所以我需要一个想法,以便我可以工作。

编辑:顺便说一下,当我试图向集合中添加一个事件时,我得到的错误是在 PaginatedBar上的错误。还没找到。

AttendeesCollection (分页)

代码语言:javascript
复制
define([
    'jQuery',
    'Underscore',
    'Backbone',
    'models/AttendeeModel',
    'libs/plugins/backbone.paginator'
],function($, _, Backbone, Attendee,Paginator){
    var AttendeesCollection = Paginator.requestPager.extend({
        model: Attendee,
        ... (Works)
    return AttendeesCollection;
});

代码语言:javascript
复制
define([
    'jQuery',
    'Underscore',
    'Backbone',
    'libs/plugins/bootstrap-dropdown',
    'text!templates/PaginationBar.phtml'
],function($, _, Backbone, twdd, PaginationBarTPL){
    var PaginationBar = Backbone.View.extend({
        events: {
            'click a.PBNext': 'nextResultPage',
            'click a.PBPrev': 'previousResultPage',
            'click a.PBSort': 'updateSortBy',
            'click a.PBPage': 'gotoPage'
        },
        initialize: function(){
            _.bindAll(this,'render','updateSortBy','nextResultPage', 'previousResultPage', 'gotoPage');
            this.collection.on('reset', this.render, this);
            this.collection.on('change', this.render, this);
        }
            (...)
    });
    return PaginationBar;
});

代码语言:javascript
复制
define([
    'jQuery',
    'Underscore',
    'Backbone',
    'facade',
    'views/PaginationBarView',
    'text!templates/attendees.phtml',
    'collections/AttendeesCollection'
],function($, _, Backbone,facade,PaginationBarView,AttendeesTPL,AttendeesCollection){
    var AttendeesView = Backbone.View.extend({

        el: "#attendees",
        collection: new AttendeesCollection,
        paginationbar: new PaginationBarView({collection: this.collection}),

        initialize: function(){

            _.bindAll(this, 'render', 'onClose');

            $(this.el).find(".paginationBar").append(this.paginationbar.render().el)
            this.collection.on('change', this.addAll, this);
            this.collection.on('reset', this.addAll, this);
            this.collection.on('add', this.addOne, this);
            this.collection.pager();
        },
              (...)
    });
    return AttendeesView;
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-20 12:56:41

在视图定义中,在PagniationBarView存在之前创建this.collection。我建议把它搬到initialize()

代码语言:javascript
复制
var AttendeesView = Backbone.View.extend({

    el: "#attendees",
    collection: new AttendeesCollection,
    paginationbar: null, // Do not create the view here

    initialize: function(){

        _.bindAll(this, 'render', 'onClose');

        // Create the view here now that this.collection exists
        this.paginationbar = new PaginationBarView({collection: this.collection});

        // ...

    }
});
return AttendeesView;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12035599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档