Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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()
like image 985
K Everest Avatar asked Feb 09 '12 01:02

K Everest


2 Answers

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

like image 197
K Everest Avatar answered Sep 20 '22 17:09

K Everest


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

like image 33
tgriesser Avatar answered Sep 18 '22 17:09

tgriesser