admin管理员组文章数量:1431726
I have a Backbone collection
jQuery ->
class App.Collections.List extends Backbone.Collection
model: App.Models.ListItem
I am trying to initialize the collection on page load:
var list = new App.Collections.List;
list.reset(<%= @data.to_json.html_safe %>)
This throws a JS error in the backbone lib.
Uncaught TypeError: undefined is not a 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 i change the code to:
var list = new Backbone.Collections;
list.reset(<%= @data.to_json.html_safe %>)
The reset works, and the collection is populated -- thought the objects in the Collection don't appear to know that they should be ListItem objects. Do I have to do something special to all a reset of my custom Collection?
I have a Backbone collection
jQuery ->
class App.Collections.List extends Backbone.Collection
model: App.Models.ListItem
I am trying to initialize the collection on page load:
var list = new App.Collections.List;
list.reset(<%= @data.to_json.html_safe %>)
This throws a JS error in the backbone lib.
Uncaught TypeError: undefined is not a 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 i change the code to:
var list = new Backbone.Collections;
list.reset(<%= @data.to_json.html_safe %>)
The reset works, and the collection is populated -- thought the objects in the Collection don't appear to know that they should be ListItem objects. Do I have to do something special to all a reset of my custom Collection?
Share Improve this question edited Jun 4, 2012 at 16:17 mu is too short 435k71 gold badges859 silver badges818 bronze badges asked Jun 4, 2012 at 16:07 empire29empire29 3,8909 gold badges49 silver badges74 bronze badges 2- Works for me, as far as I can reproduce your setup : jsfiddle/LXW6h – nikoshr Commented Jun 4, 2012 at 16:15
- The Model and Collection load order was incorrect. See Derick's answer below and my followup ment to hom. – empire29 Commented Jun 4, 2012 at 16:50
2 Answers
Reset to default 7the _prepareModel
stacktrace line gives a hint that you have your model declared after your collection.
You most likely have your code set up like this:
class App.Collections.List extends Backbone.Collection
model: App.Models.ListItem
class App.Models.ListItem extends Backbone.Model
which is going to fail because ListItem
is not yet declared when you try to use it in your collection's model
attribute. You are essentially setting the model attribute to undefined
.
You need to declare the model first:
class App.Models.ListItem extends Backbone.Model
class App.Collections.List extends Backbone.Collection
model: App.Models.ListItem
Note that this is not a limitation in CoffeeScript or Backbone. This is a JavaScript behavior caused by the use of object literals. The value of an object literal key/value pair is evaluated immediately, which means it must exist or it will be returned as undefined or some other error thrown.
As Derick said you need to declare models first, for that purpose just I usually just change require order the App.coffee(main backbone file) to this
...
require_tree ./models
require_tree ./collections
...
本文标签: javascriptBackbone Collectionreset() doesn39t work on extended CollectionsStack Overflow
版权声明:本文标题:javascript - Backbone Collection.reset() doesn't work on extended Collections - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745525149a2661801.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论