[英]Backbone Error: Uncaught TypeError: Object function (){ parent.apply(this, arguments); } has no method 'on'
Any ideas why am I getting this error when I invoke collection.fetch
? 任何想法,为什么我在调用collection.fetch
时收到此错误?
It's thrown in this section of the code: 它出现在代码的这一部分:
This is the code that triggers the error: 这是触发错误的代码:
$(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()
The problem was with this line of code: 问题在于这行代码:
SearchResult = Backbone.Model.extend
It should have been like this: 应该是这样的:
SearchResult = Backbone.Model.extend()
Otherwise CoffeeScript was assigning the extend
function to SearchResult 否则CoffeeScript将extend
功能分配给SearchResult
You're not actually attaching the models to the collection... 你实际上并没有将模型附加到集合中......
from the docs, parse should 从文档中,解析应该
return the array of model attributes to be added to the collection. 返回要添加到集合的模型属性数组。
$(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()
I haven't tested it, but i believe that will work 我没有测试过,但我相信这会有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.