我在调用collection.fetch
时遇到此错误的任何想法?
这是代码的这一部分:
这是触发错误的代码:
$(document).ready ->
SearchResult = Backbone.Model.extend
SearchResults = Backbone.Collection.extend
url: "/backbone/search"
model: SearchResult
parse: (response)->
console.log response
new SearchResult
id: response.id
title: response.title
searchResults = new SearchResults()
searchResults.fetch()
答案 0 :(得分:8)
问题在于这行代码:
SearchResult = Backbone.Model.extend
应该是这样的:
SearchResult = Backbone.Model.extend()
否则CoffeeScript将extend
函数分配给SearchResult
答案 1 :(得分:2)
您实际上并未将模型附加到集合中......
来自文档,解析应该
返回要添加到集合中的模型属性数组。
$(document).ready ->
SearchResult = Backbone.Model.extend
SearchResults = Backbone.Collection.extend
url: "/backbone/search"
model: SearchResult
parse: (response) ->
_.map response, (item) ->
id: item.id
title: item.title
searchResults = new SearchResults()
searchResults.fetch()
我没有测试过,但我相信会有效