Javascript 将集合传递给多个视图
我正在尝试将分页的集合(Javascript 将集合传递给多个视图,javascript,collections,backbone.js,pagination,backbone-views,Javascript,Collections,Backbone.js,Pagination,Backbone Views,我正在尝试将分页的集合(backbone.paginatorplugin)传递给多个视图,以便共享它,并且我可以使用同一集合调用我的三个视图的函数 但是,我的主视图(AttendeesView)正在调用其他视图(PaginationBarView和AttendeeView)。 我试图通过我的视图的构造器和路由器传递它。 我似乎不能让它工作,所以我需要一个想法,这样我就可以工作 EDIT:顺便说一句,我得到的错误是uncaughttypeerror:当我尝试向集合中添加事件时,无法在分页数据库上调
backbone.paginator
plugin)传递给多个视图,以便共享它,并且我可以使用同一集合调用我的三个视图的函数
但是,我的主视图(AttendeesView
)正在调用其他视图(PaginationBarView
和AttendeeView
)。
我试图通过我的视图的构造器和路由器传递它。
我似乎不能让它工作,所以我需要一个想法,这样我就可以工作
EDIT:顺便说一句,我得到的错误是uncaughttypeerror:当我尝试向集合中添加事件时,无法在分页数据库上调用未定义的方法“on”。没有找到
与会者集合
(分页)
您正在视图定义中创建PagniationBarView
,然后才存在此.collection
。我建议将其移动到initialize()
:
如果您不只是复制/粘贴您的应用程序代码,而是在最简单的代码示例中重新创建问题,我们将不胜感激。对不起,在离开工作之前,我没有太多时间问我的问题。但是如果真的让你烦恼的话,你可以编辑它。谢谢,这就是问题所在,在我看到你的消息之前,一位同事刚刚解决了这个问题。现在我知道我不会初始化任何变量,这取决于我的初始化之外的其他变量。。。
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;
});
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;