简体   繁体   English

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

[英]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.

相关问题 Backbone.js和Require.js-未捕获的TypeError:对象函数(){return parent.apply(this,arguments); }没有方法“开” - Backbone.js and Require.js - Uncaught TypeError: Object function (){ return parent.apply(this, arguments); } has no method 'on' 未捕获的TypeError:对象函数(){返回i.apply(this,arguments)}没有方法&#39;on&#39; - Uncaught TypeError: Object function (){return i.apply(this,arguments)} has no method 'on' Sencha:未捕获的TypeError:对象函数(){h.apply(this,arguments)}没有方法&#39;setActiveItem&#39; - Sencha : Uncaught TypeError: Object function (){h.apply(this,arguments)} has no method 'setActiveItem' 未捕获的TypeError:Object# <Object> 没有方法&#39;apply&#39;错误 - Uncaught TypeError: Object #<Object> has no method 'apply' error Backbone.js - 未捕获TypeError:对象[object Object]没有方法&#39;apply&#39; - Backbone.js - Uncaught TypeError: Object [object Object] has no method 'apply' Backbone.js:未捕获的TypeError:对象[object Object]没有方法&#39;apply&#39; - Backbone.js: Uncaught TypeError: Object [object Object] has no method 'apply' Backbone.Model =&gt;未捕获的TypeError:对象[Object object]没有方法&#39;apply&#39; - Backbone.Model => Uncaught TypeError: Object [Object object] has not method 'apply' 未捕获的TypeError:对象(JS函数)没有方法“应用” - Uncaught TypeError: Object (JS Function) has no method 'apply' 未捕获的TypeError:对象.NumCon没有方法'apply' - Uncaught TypeError: Object .NumCon has no method 'apply' 骨干-未捕获的TypeError:对象[object Object]没有方法&#39;call&#39; - Backbone - Uncaught TypeError: Object [object Object] has no method 'call'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM