[英]Backbone.js Collection of Collections
I'm trying to figure out how to make a Collection of collections with backbone.js. 我正在试图弄清楚如何使用backbone.js创建集合集合。 I'm pretty new to backbone.
我对骨干很新。 I have something like the following situation:
我有类似以下情况:
+---------------+ +------------------+
| Playlists | | Playlist |
|---------------| 0..* |------------------|
| +-------------->| Name |
| | | |
| | | |
+---------------+ +-------+----------+
|
|
|0..*
v
+------------------+
| Track |
|------------------|
| Name |
| Artist |
| |
+------------------+
In code this looks similar to this: 在代码中,这看起来类似于:
var trackModel = Backbone.Model.extend({
//trackdata
});
var playlistModel = Backbone.Collection.extend({
model : trackModel,
url : "playlist"
});
var playlistsModel = Backbone.Collection.extend({
url : "playlists",
model : playlistModel //This pretty sure doesn't work like I want, because there is no model attribute for collections :S
});
However I always receive an error in the js console saying: 但是我总是在js控制台中收到错误说:
Uncaught TypeError: Object [object Object] has no method '_validate'
when I try to execute a function that triggers the validate (like add, fetch, ...) 当我尝试执行一个触发验证的函数(如add,fetch,...)
It makes no difference if i add the validate
or _validate
function to any of the collections or models. 如果我将
validate
或_validate
函数添加到任何集合或模型中没有区别。
I believe this is because backbone.js doesn't support collections in collections. 我相信这是因为backbone.js不支持集合中的集合。 Is there another way that works?
还有另一种方法吗?
UPDATE: 更新:
This is how it looks right now 这就是它现在的样子
var Track = Backbone.Model.extend({
//trackdata
});
var Tracks = Backbone.Collection.extend({
model:Track;
});
var Playlist = Backbone.Model.extend({
//name : ...
tracks: new Tracks ()
});
var Playlists = Backbone.Collection.extend({
url : "playlists",
model : Playlist
});
You'd solve your problem by turning your Playlist
from a collection into a model. 您可以通过将
Playlist
从集合转换为模型来解决问题。 If you think about it, a Playlist
would probably have other attributes anyway (eg name) that wouldn't be settable on a collection. 如果您考虑一下,
Playlist
可能还有其他属性(例如名称),这些属性无法在集合中设置。
Playlists
would then be a collection of Playlist
models (instead of collections), which should work without error. 然后
Playlists
将是Playlist
模型(而不是集合)的集合,这应该没有错误地工作。
var Track = Backbone.Model.extend({
//trackdata
});
var Playlist = Backbone.Model.extend({
model : Track
});
var Playlists = Backbone.Collection.extend({
url : "playlists",
model : Playlist
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.