Backbone Error:Uncaught TypeError:Object function(){parent.apply(this,arguments);没有'on'方法

时间:2012-02-09 01:14:38

标签: javascript backbone.js coffeescript

我在调用collection.fetch时遇到此错误的任何想法?

这是代码的这一部分:

Backbone 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()

2 个答案:

答案 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()

我没有测试过,但我相信会有效