Bog*_*Biv 3 javascript backbone.js backbone-events
这似乎是一个集合的标准用法,但它不起作用.我在文档就绪函数中有以下代码:
$(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();
});
Run Code Online (Sandbox Code Playgroud)
我已经使用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});
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
作为旁注,如果您需要更多上下文,我正在尝试关注Chris Storm的教程/链.
相关软件版本:
更新:稍微调试一下,我发现在抛出错误时,this.model有值function Event() { [native code] }
并且调用new Event()
会引发更多错误 - TypeError
.那么创建事件有什么问题?
你的问题是你(和Chris Storm)只为你的模型选择了一个坏名字.JavaScript中已有一个事件(至少在连接到DOM的JavaScript中); 此外,您不能调用new Event()
创建DOM事件对象,您应该使用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;
});
Run Code Online (Sandbox Code Playgroud)