2013-02-12 17 views
0

這是我的觀點集合:如何在索引視圖上添加微調以在Backbone中獲取?

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

,但我得到:

Uncaught TypeError: Cannot call method 'removeClass' of undefined 
+0

也許加上'等待:TRUE'而您的收藏獲取數據 – 2013-02-12 14:47:42

回答

3
this.$el.append('<div class="loading">Loading...</div>'); 

添加一個div一個類加載在你的$ el元素中。

this.$el.removeClass('loading'); 

removeClass不會刪除具有指定類的元素中的元素。

嘗試:

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

啊,因爲它只是jQuery的呢!好的電話,謝謝。 – thatmiddleway 2013-06-05 22:00:42

1

你得到TypeError,因爲$el未定義在this內的success回調中。這是因爲this內的success回調不是指你的Index查看。

您需要綁定this到回調使this指的回調中的Index查看..

success:function() { 
    this.$el.removeClass('loading'); 
}.bind(this) 

閱讀bind on MDN

相關問題