javascript - 每个都不在 Backbone View 渲染中工作

标签 javascript json backbone.js

我刚开始使用 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"
}

最佳答案

this.collection = ShowCollection;

应该是

this.collection = new ShowCollection();

关于javascript - 每个都不在 Backbone View 渲染中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15466430/

相关文章:

javascript - 可以覆盖 fabricjs 中的边界框选择区域 - 控制选项

android - 如何在 GSON TypeToken 类中使用自定义泛型?

backbone.js - 辅助函数在 Backbone 中的位置

javascript - Backbone.js 添加到集合

Backbone.js model.save() - 正确的服务器响应

javascript - jquery在一个元素内向上滑动两个元素并向下滑动一个元素

javascript - jquery 单击处理程序和面向对象编程

javascript - 如何使用 Three.JS 通过单键按下制作动画

c# - 可以查找 Json.net 中不存在的 Key

javascript - 将 JSON 数组转换为 html