收藏的Backbone.js的集合 [英] Backbone.js Collection of Collections

查看:102
本文介绍了收藏的Backbone.js的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何使Backbone.js的集合的集合。我是pretty新骨干。我有一些像下面这种情况:

  + -------- + + + ------------------
 |播放列表| |播放列表|
 | --------------- | 0 .. * | ------------------ |
 | + --------------> |名称|
 | | | |
 | | | |
 + --------------- + ------- + + ---------- +
                                         |
                                         |
                                         | 0 .. *
                                         v
                                 + ------------------ +
                                 |追踪|
                                 | ------------------ |
                                 |名称|
                                 |艺术家|
                                 | |
                                 + ------------------ +

在code这看起来类似于这样:

  VAR trackModel = Backbone.Model.extend({
    // trackdata
});VAR playlistModel = Backbone.Collection.extend({
    型号:trackModel,
    网址:播放列表
});VAR playlistsModel = Backbone.Collection.extend({
    网址:播放列表,
    型号:playlistModel //这pretty肯定行不通像我想要的,因为对集合没有模型属性:•
});

不过,我总是收到一个错误的JS控制台说:

 未捕获类型错误:对象的翻译:没有方法'_Validate

当我尝试执行触发验证功能(如添加,获取,...)

这没有什么区别,如果我添加了验证 _Validate 函数的集合或模型。

我相信这是因为Backbone.js的不支持的集合在集合。是否有工作?

另一种方式

更新:

这是它的外观现在

  VAR轨道= Backbone.Model.extend({
    // trackdata
});VAR曲目= Backbone.Collection.extend({
    型号:跟踪;
});VAR播放列表= Backbone.Model.extend({
    //名称 : ...
    轨道:新曲目()
});VAR播放列表= Backbone.Collection.extend({
    网址:播放列表,
    型号:播放列表
});


解决方案

您会通过转动你的播放列表从集合到模型解决您的问题。如果你想想看,一个播放列表或许会反正其他属性(如名称),不会对集合设置的。

播放列表然后将播放列表模式(而不是集合)的集合,这应该没有错误工作。

  VAR轨道= Backbone.Model.extend({
    // trackdata
});VAR播放列表= Backbone.Model.extend({
    型号:轨道
});VAR播放列表= Backbone.Collection.extend({
    网址:播放列表,
    型号:播放列表
});

I'm trying to figure out how to make a Collection of collections with 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:

 Uncaught TypeError: Object [object Object] has no method '_validate'

when I try to execute a function that triggers the validate (like add, fetch, ...)

It makes no difference if i add the validate or _validate function to any of the collections or models.

I believe this is because backbone.js doesn't support collections in collections. 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. If you think about it, a Playlist would probably have other attributes anyway (e.g. name) that wouldn't be settable on a collection.

Playlists would then be a collection of Playlist models (instead of collections), which should work without error.

var Track = Backbone.Model.extend({
    //trackdata
});

var Playlist = Backbone.Model.extend({
    model : Track
});

var Playlists = Backbone.Collection.extend({
    url   : "playlists",
    model : Playlist
});

这篇关于收藏的Backbone.js的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆