我试图弄清楚如何使用backbone.js 制作集合集合。我对骨干很陌生。我有类似以下情况:
+---------------+ +------------------+
| Playlists | | Playlist |
|---------------| 0..* |------------------|
| +-------------->| Name |
| | | |
| | | |
+---------------+ +-------+----------+
|
|
|0..*
v
+------------------+
| Track |
|------------------|
| Name |
| Artist |
| |
+------------------+
在代码中,这看起来类似于:
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
});
但是我总是在 js 控制台中收到错误消息:
Uncaught TypeError: Object [object Object] has no method '_validate'
当我尝试执行触发验证的函数时(如添加、获取、...)
如果我将validate
or_validate
函数添加到任何集合或模型中,这没有区别。
我相信这是因为backbone.js 不支持集合中的集合。还有另一种可行的方法吗?
更新:
这就是它现在的样子
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
});