Javascript 如何在索引视图上添加微调器以获取主干?
这是我的收藏视图:Javascript 如何在索引视图上添加微调器以获取主干?,javascript,backbone.js,spinner,Javascript,Backbone.js,Spinner,这是我的收藏视图: Network.Views.Offers.Index = Backbone.View.extend({ initialize: function() { _.bindAll(this, 'render'); this.collection = new Network.Collections.Offers(); this.collection .on('reset', this.render) .fetch(); }, re
Network.Views.Offers.Index = Backbone.View.extend({
initialize: function() {
_.bindAll(this, 'render');
this.collection = new Network.Collections.Offers();
this.collection
.on('reset', this.render)
.fetch();
},
render: function() {
var self = this;
_.each(this.collection.models, function(model) {
var view = new Network.Views.Offers.Offer({ model: model });
$(self.el).append(view.el);
view.adjustHeight();
});
return this;
},
});
我尝试在成功获取后添加和删除微调器类:
this.$el.append('<div class="loading">Loading...</div>');
this.collection
.on('reset', this.render)
.fetch({
success:function() {
this.$el.removeClass('loading');
}
});
您得到类型错误是因为在
success
回调中this
上未定义$el
。这是因为成功
回调中的此
未引用您的索引
视图
您需要将此
绑定到该回调,以使此
引用该回调中的索引
视图
success:function() {
this.$el.removeClass('loading');
}.bind(this)
阅读有关的内容,您会收到类型错误,因为
$el
未在成功回调中的上定义。这是因为成功
回调中的此
未引用您的索引
视图
您需要将此
绑定到该回调,以使此
引用该回调中的索引
视图
success:function() {
this.$el.removeClass('loading');
}.bind(this)
了解
removeClass不会删除具有指定类的元素中的元素
尝试:
this.$el.append('Loading…');
var$this=this;//在成功回调中维护此的上下文
这是我的收藏
.on('reset',this.render)
.取回({
成功:函数(){
$this.$('.loading').remove();
}
});
removeClass不会删除具有指定类的元素中的元素
尝试:
this.$el.append('Loading…');
var$this=this;//在成功回调中维护此的上下文
这是我的收藏
.on('reset',this.render)
.取回({
成功:函数(){
$this.$('.loading').remove();
}
});
当您的集合获取数据时,可以添加等待:true
当您的集合获取数据时,可以添加等待:true
this.$el.append('<div class="loading">Loading...</div>');
this.$el.removeClass('loading');
this.$el.append('<div class="loading">Loading...</div>');
var $this = this; // maintain the context of this within the success callback
this.collection
.on('reset', this.render)
.fetch({
success:function() {
$this.$('.loading').remove();
}
});