javascript - Backbone Collection.reset() doesn't work on extended Collections -
i have backbone collection
jquery -> class app.collections.list extends backbone.collection model: app.models.listitem i trying initialize collection on page load:
var list = new app.collections.list; list.reset(<%= @data.to_json.html_safe %>) this throws js error in backbone lib.
uncaught typeerror: undefined not function application.js:597 f.extend._preparemodel application.js:597 f.extend.add application.js:591 f.extend.reset application.js:595 (anonymous function) however, if change code to:
var list = new backbone.collections; list.reset(<%= @data.to_json.html_safe %>) the reset works, , collection populated -- thought objects in collection don't appear know should listitem objects. have special reset of custom collection?
the _preparemodel stacktrace line gives hint have model declared after collection.
you have code set this:
class app.collections.list extends backbone.collection model: app.models.listitem class app.models.listitem extends backbone.model which going fail because listitem not yet declared when try use in collection's model attribute. setting model attribute undefined.
you need declare model first:
class app.models.listitem extends backbone.model class app.collections.list extends backbone.collection model: app.models.listitem note not limitation in coffeescript or backbone. javascript behavior caused use of object literals. value of object literal key/value pair evaluated immediately, means must exist or returned undefined or other error thrown.
Comments
Post a Comment