Backbone.js el主干视图的问题
我不明白为什么在我看来el只定义在loadResults函数中,而不是在checkScroll中…可能取决于documentAddEventLIstener(“scroll”,this.checkScroll,this)? 我不明白为什么Backbone.js el主干视图的问题,backbone.js,Backbone.js,我不明白为什么在我看来el只定义在loadResults函数中,而不是在checkScroll中…可能取决于documentAddEventLIstener(“scroll”,this.checkScroll,this)? 我不明白为什么 var HomePostView = Backbone.View.extend({ tagName: "ul", // id: "list", // el:$('.table-view'), template: Handl
var HomePostView = Backbone.View.extend({
tagName: "ul",
// id: "list",
// el:$('.table-view'),
template: Handlebars.compile(template),
events: {
'scroll': 'checkScroll'
},
initialize: function () {
//console.log(this.collection);
// this.collection.bind("add", this.render, this);
document.addEventListener("scroll", this.checkScroll, this);
this.isLoading = false;
this.IntegratedCollection= new Integrated();
this.IntegratedCollection.twitterQuery=11265832;//spostare in load results
this.IntegratedCollection.fetch();
this.listenTo(this.IntegratedCollection,'add',this.render);
console.log((this.el));
},
render:function(){
this.loadResults();
},
loadResults: function (eventName) {
console.log((this.el));<-----WELL DEFINED HERE
this.isLoading = true;
$(this.el).empty();
_.each(this.IntegratedCollection.models, function (a) {
$(this.el).append(new SingleHomePostView({
model: a
}).render().el);
}, this);
this.isLoading = false;
return this;
},
setParameters: function(){
this.IntegratedCollection.page += 1; // Load next page
this.IntegratedCollection.twitterQuery=11265832;
this.IntegratedCollection.fetch();
},
checkScroll: function () {
console.log(this.el);<-----UNDEFINED HERE
var triggerPoint = 100; // 100px from the bottom
if( !this.isLoading && this.el.scrollTop + this.el.clientHeight + triggerPoint > this.el.scrollHeight ) {
console.log("inside");
// this.setParameters();
//this.loadResults();
}
}
});
var HomePostView=Backbone.View.extend({
标记名:“ul”,
//id:“列表”,
//el:$(“.表视图”),
模板:把手。编译(模板),
活动:{
“滚动”:“检查滚动”
},
初始化:函数(){
//console.log(this.collection);
//this.collection.bind(“add”、this.render、this);
document.addEventListener(“滚动”,this.checkScroll,this);
this.isLoading=false;
this.IntegratedCollection=new Integrated();
this.IntegratedCollection.twitterQuery=11265832;//加载结果中的spostare
this.IntegratedCollection.fetch();
this.listenTo(this.IntegratedCollection,'add',this.render);
console.log((this.el));
},
render:function(){
这是loadResults();
},
loadResults:函数(eventName){
console.log((this.el));我认为您的“this”指向的是窗口对象。试试看
that = this;//In initialize
并在checkScroll中使用that
而不是this
。这是因为:
document.addEventListener("scroll", this.checkScroll, this);
DOMaddEventListener
函数接受三个参数,但第三个参数不是上下文参数,它是一个布尔值,告诉它是否在事件传播的捕获阶段处理事件。相反,您可以将事件处理程序与适当的上下文绑定:
document.addEventListener("scroll", this.checkScroll.bind(this));