BackboneJS 如何在视图中使用参数
BackboneJS How to use parameter inside View
我有一个 Backbone 集合,其中 returns 给定数量的模型,现在我有一个 "load More" 按钮,可以加载更多模型。加载的集合基于参数。我的问题是,当加载 Collection 时,在 Views initialize
函数中我能够获取 Collection 所基于的参数。不过,我的 "loadMore" 函数需要这个参数。所以,我的问题是,如何将参数从 initialize
函数传递给另一个函数?
这是我目前得到的:
MyStuff.Collection = Backbone.Collection.extend({
url: 'someUrl',
initialize: function(opts) {
opts = opts || {};
this.value= opts.value;
},
step: 0,
parse: function(response){
var slice = response.results.slice(this.step*8,(this.step+1)*8);
this.step++;
return slice;
}
});
MyStuff.View= Backbone.View.extend({
template: 'myTemplate',
initialize: function() {
this.currentPage = 0;
this.listenTo(this.collection, 'sync', this.hideLoader);
this.listenTo(this.collection, 'reset', this.afterRender);
this.collection.reset(this.collection.shuffle(), {silent:true});
var suit = this.collection.toJSON()[0]; // HERE I GET MY VALUE!!
},
events: {
'click .loadMore': 'loadMore'
}
loadMore: function(opts){
this.showLoader();
this.currentPage++;
this.collection.fetch({url: this.collection.url + '?page='+ this.currentPage + '&value=' // HERE I NEED MY VALUE!!!! //, remove: false});
this.collection.fetch({remove: false});
this.render();
}
});
return MyStuff;
this.suit = this.collection.toJSON()[0].nameOfYourKey;
'&value=' + this.suit
您可以访问放在 this
上的内容,这是您的视图实例指针。
我有一个 Backbone 集合,其中 returns 给定数量的模型,现在我有一个 "load More" 按钮,可以加载更多模型。加载的集合基于参数。我的问题是,当加载 Collection 时,在 Views initialize
函数中我能够获取 Collection 所基于的参数。不过,我的 "loadMore" 函数需要这个参数。所以,我的问题是,如何将参数从 initialize
函数传递给另一个函数?
这是我目前得到的:
MyStuff.Collection = Backbone.Collection.extend({
url: 'someUrl',
initialize: function(opts) {
opts = opts || {};
this.value= opts.value;
},
step: 0,
parse: function(response){
var slice = response.results.slice(this.step*8,(this.step+1)*8);
this.step++;
return slice;
}
});
MyStuff.View= Backbone.View.extend({
template: 'myTemplate',
initialize: function() {
this.currentPage = 0;
this.listenTo(this.collection, 'sync', this.hideLoader);
this.listenTo(this.collection, 'reset', this.afterRender);
this.collection.reset(this.collection.shuffle(), {silent:true});
var suit = this.collection.toJSON()[0]; // HERE I GET MY VALUE!!
},
events: {
'click .loadMore': 'loadMore'
}
loadMore: function(opts){
this.showLoader();
this.currentPage++;
this.collection.fetch({url: this.collection.url + '?page='+ this.currentPage + '&value=' // HERE I NEED MY VALUE!!!! //, remove: false});
this.collection.fetch({remove: false});
this.render();
}
});
return MyStuff;
this.suit = this.collection.toJSON()[0].nameOfYourKey;
'&value=' + this.suit
您可以访问放在 this
上的内容,这是您的视图实例指针。