主干错误:未捕获类型错误:对象函数(){parent.apply(this,arguments); } 没有方法“on”;

发布于 2025-01-03 10:57:54 字数 553 浏览 2 评论 0原文

任何想法为什么我在调用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()

Any ideas why am I getting this error when I invoke collection.fetch?

It's thrown in this section of the code:

Backbone Error

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半透明的墙 2025-01-10 10:57:54

问题出在这行代码上:

SearchResult = Backbone.Model.extend

它应该是这样的:

SearchResult = Backbone.Model.extend()

否则 CoffeeScript 会将 extend 函数分配给 SearchResult

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

栀梦 2025-01-10 10:57:54

您实际上并没有将模型附加到集合中...

从文档中,解析应该

返回要添加到集合中的模型属性数组。

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

我还没有测试过,但我相信这会起作用

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文