Any ideas why am I getting this error when I invoke 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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With