3

这似乎几乎是集合的标准用法,但它不起作用。我在文档就绪函数中有以下代码:

$(function() {
  window.EventList = Backbone.Collection.extend({
    model: Event,
    url: '/events',
        parse: function(response) { // same result with or without parse function
      return _(response.rows).map(function(row) { return row.doc ;});
    }
  });         
  window.Events = new EventList;
  // throws Uncaught TypeError: Illegal constructor
  // Events.fetch();
});

我用 Chromium 和 Firefox 测试过,FF 更冗长:

this.model is not a constructor at Backbone.js line:570
[Break On This Error] model = new this.model(attrs, {collection: this}); 

我错过了什么?

作为旁注,如果您需要更多上下文,我正在尝试遵循Chris Storm 的教程/链。

相关软件版本:

  • jQuery JavaScript 库 v1.7.1
  • 骨干网.js 0.5.3
  • 下划线.js 1.2.2
  • Chromium 14.0.835.202(开发者内部版本 103287 Linux)Ubuntu 11.10
  • Firefox 是 8.0,Ubuntu 11.10,64 位

更新:再调试一下,我发现在抛出错误时, this.model 具有值function Event() { [native code] }并且调用new Event()会抛出更多错误 - TypeError。那么创建事件有什么问题呢?

4

1 回答 1

7

您的问题是您(和 Chris Storm)只是为您的模型选择了一个坏名字。JavaScript中已经有一个事件(至少在连接到 DOM的 JavaScript 中);此外,您不能调用new Event()创建 DOM Event 对象,您应该使用document.createEvent('Event').

尝试将您的事件重命名为 CalendarEvent:

$(function() {
  window.CalendarEvent     = Backbone.Model.extend({});
  window.CalendarEventList = Backbone.Collection.extend({
    model: CalendarEvent,
    url: '/events',
    parse: function(response) {
      return _(response.rows).map(function(row) { return row.doc ;});
    }
  });         
  window.CalendarEvents = new CalendarEventList;
});
于 2011-12-04T19:28:21.090 回答