简体   繁体   中英

how to model this relationship in Spine.js

in my app i would need to model a relationship between objects that is very similar to a filesystem.

i have Image , Video and Collection

Collections can contain all of the three type of Objects. while a Image and Video can be part of multiple Collections, the Collection can only be contained in one (1) Collection.

what is the best way to model this kind of relationship in spine?

what i thought to do is make everything sublclass from one generic model definition, lets say Asset so everything is an Asset and i can implement class and instancemethods on that model and all subclassed model definitions inherit those methods.

the problem i encountered with this approach is that i would need/like to put the relationship on Asset so

class Asset extends Spine.Model
    @configure 'Asset', 'title', 'description'

    @belongsTo 'collection', Collection

besides the fact that i am not sure if this actually works if i have Colletion subclass Asset and asset sets a reference to Collection... the problem here is that belongsTo is a one way relation, and i need this only for Collections so the right way would be to have a @hasMany 'collections', Collection and be sure no collection sets more than one object to this property

another question is: can i use Asset.find(id) to get all objects that are a subclass of Asset?

I'm not really sure what your problem is, other than trying out if your above structure works.

What you're describing is known as the 'composite design pattern' ( http://en.wikipedia.org/wiki/Composite_pattern ) where Collection is the 'composite' and both Image and Video are the 'leafs'

Relating to your @hasMany question, wouldn't you want the following instead of @hasMany 'collections', Collection on your Collection subclass, in addition to the Asset code you already defined:

@hasMany 'assets', Asset

Given that you could traverse up and down the tree.

Again, I'm really not sure if such recursive traversal works in Spine (never tried it, although I use Spine extensively)

hth some, Geert-Jan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM