Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在索引视图上添加微调器以获取主干?_Javascript_Backbone.js_Spinner - Fatal编程技术网

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