【问题标题】:Passing a collection to multiple views将集合传递给多个视图
【发布时间】:2012-08-20 09:56:22
【问题描述】:

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

但是,我的主视图 (AttendeesView) 正在调用其他视图(PaginationBarViewAttendeeView 每页)。 我试图通过我的视图的构造函数通过路由器传递它。 我似乎无法让它发挥作用,所以我需要一个想法,这样我才能继续努力。

编辑:顺便说一句,当我尝试将事件添加到我的收藏时,我在 PaginatedBar 上得到的错误是 Uncaught TypeError: Cannot call method 'on' of undefined。没找到。

AttendeesCollection(分页)

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

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;
});

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;
});

【问题讨论】:

  • 如果您努力不只是复制/粘贴您的应用程序代码,而是在最小的代码示例中重新创建问题,我们将不胜感激。
  • 抱歉,下班前我没有多少时间问我的问题了。但如果它真的让你感到困扰,你可以编辑它。

标签: javascript collections backbone.js pagination backbone-views


【解决方案1】:

this.collection 存在之前,您正在视图定义中创建PagniationBarView。我建议将其移至 initialize():

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;

【讨论】:

  • 谢谢,这是问题所在,在我看到您的消息之前,一位同事才发现它。现在我知道我不会根据其他人初始化任何 var...
猜你喜欢
  • 2018-12-08
  • 2011-12-21
  • 2011-10-01
  • 2014-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-05
相关资源
最近更新 更多