2013-03-17 47 views
1

我剛剛開始使用Backbone,並且在從某些JSON數據生成簡單的html列表時遇到問題。每個在主幹視圖中都不起作用渲染

,我發現了錯誤

Uncaught TypeError: Object function(){return c.apply(this,arguments)} has no method 'each'

這裏是我的代碼

var Show = Backbone.Model.extend(); 

var ShowCollection = Backbone.Collection.extend({ 

    model: Show, 
    url: 'http://192.168.0.7:8081/api/0b08ecef4eda8c6a28b6be3164a96ac8/?cmd=history&type=downloaded&limit=50', 

    parse: function(response){ 
     return response.data; 
    } 

}); 

var ItemView = Backbone.View.extend({ 

    tagName: "li", 
    template: $("#item").html(), 

    render: function() { 
    var templ = _.template(this.template); 
    this.$el.html(templ(this.model.toJSON())); 
    return this; 
    } 

}); 

var ShowView = Backbone.View.extend({ 

    el: $("#history"), 
    initialize: function() { 
    this.collection = ShowCollection; 
    this.render(); 
    }, 
    render: function() { 
    this.collection.each(function(item) { 
     this.renderItem(item); 
    }, this); 
    }, 
    renderItem: function(item) { 
    var itemView = new ItemView({ model: item }); 
    this.$el.append(itemView.render().el); 
    } 

}); 

var history = new ShowView(); 

這裏是我的數據

{ 
data: [ 
{ 
date: "2013-03-16 05:14", 
episode: 10, 
provider: "-1", 
quality: "HD TV", 
resource: "bering.sea.gold.s02e10.720p.hdtv.x264-bajskorv.mkv", 
resource_path: "/Users/Machine/Tv/Bering.Sea.Gold.S02E10.720p.HDTV.x264-BAJSKORV repost", 
season: 2, 
show_name: "Bering Sea Gold", 
status: "Downloaded", 
tvdbid: 254203 
} 
], 
message: "", 
result: "success" 
} 

回答

2

this.c ollection = ShowCollection;

應該是

this.collection =新ShowCollection();

+0

是的我正在使用下劃線。 – Plasticated 2013-03-17 21:43:12

+0

其實看着這條線...... this.collection = ShowCollection; 它應該可能讀取 this.collection = new ShowCollection(); 實例化對象 – 2013-03-17 21:43:53

+0

嘿@Plasticated,你應該標記答案被接受,如果它幫助你解決你的問題。 – 2013-03-18 22:42:48

1

您正在將this.collection分配給擴展的Backbone Collection類,而不是其實例。請參閱擴展文檔an example。你應該有類似this.collection = new ShowCollection()