主干集合不会填充

Backbone Collection Will Not Populate

本文关键字:填充 集合      更新时间:2023-09-26

我是backbone.js的初学者,很难找到一个非常基本的虚拟示例。我确信这是一个愚蠢错误的结果,但如果有任何指导,我将不胜感激。

这是我的javascript:

var lr = lr || {};
lr.Event = Backbone.Model.extend({});
lr.Events = Backbone.Collection.extend({
  model: lr.Event,
  url: '/json/dummy.json'
});
lr.EventView = Backbone.View.extend({
  tagName: 'div',
  initialize: function() {
    _.bindAll(this, "render");
    this.collection = new lr.Events();
    this.collection.fetch();
    console.log(this.collection.toJSON());
    this.render();
  },
  render: function() {
  } 
});
lr.ev = new lr.EventView();

这里是json/dummy.json:

[
  { "title": "Event1", "Location": "New York, NY" },
  { "title": "Event2", "Location": "Cleveland, OH" }
]

我希望这将console.log作为JSON,但console.log是一个空数组。我做错了什么?

您实际上需要等待数据被检索,因为获取是异步的。cofescription中的示例:

collection.fetch({
  success: (model, resp) ->   
     console.log();
})