0

backbone.paginatorページ化されたコレクション(プラグイン)を複数のビューに渡して共有し、同じコレクションを使用して3つのビューの関数を呼び出せるようにしようとしています。

ただし、他のビュー(およびページごとAttendeesView)を呼び出している私のメインビュー()。ビューのコンストラクター、ルーターによってそれを渡そうとしました。私はそれを機能させることができないように思われるので、私はそれに取り組むことができるようにアイデアが必要になるでしょう。PaginationBarViewAttendeeView

編集:ちなみに、Uncaught TypeError: Cannot call method 'on' of undefinedコレクションにイベントを追加しようとすると、PaginatedBarでエラーが発生します。見つかりません。

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

1 に答える 1

1

PagniationBarViewが存在する前に、ビュー定義でを作成していますthis.collection。私はそれをに移動することをお勧めします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;
于 2012-08-20T12:56:41.703 に答える