vendredi 24 avril 2015

How to implement many-to-many relation in seperate model in Ember Data

I've been searching for days to get my many-to-many data working in Ember, non of the provided ways (mixin, polymorphic) seem to work for me - or I didnt understand them. Here's what I got: 3 Data(base)-Tables an Artist, a Track and one that connects them as many to many with an additional property.

My Models are these:

App.Artist = DS.Model.extend({
    name: DS.attr('string'),
    tracks: DS.hasMany('artist2Track', { async: true })
});

App.Track = DS.Model.extend({
    bez: DS.attr('string'),
});

App.Artist2Track = DS.Model.extend({
    artist: DS.belongsTo('artist', {async: true}),
    track: DS.belongsTo('track', {async: true}),
    type: DS.attr('string')
});

And the data (now in Fixtures) would be something like:

App.Artist.FIXTURES = [
{
    id: 1,
    name: "Max Mustermann",
    tracks: [5]
}
];
App.Track.FIXTURES = [
{
    id: 5,
    bez: 'Keyboard',
    artists: [1]
}
];
App.Artist2Track.FIXTURES = [
{
    artist: 1,
    track: 5,
    type: 'Principal'
}
];

Now, since Ember relies on an unique ID called 'id' I cannot access the Artist2Track-Model from both sides.
I already started to "double" the relation-table (Artist2Track) with a correct property called 'id', one for each side; but that doesnt seem to be the right way, and I suppose synchronizing the data will be a mess.

So, the question is:
Is there a way to tell Ember to not look for 'id' but for 'artist' as the id when coming from the artist-side, and vice-versa looking for 'track' when coming from the other side?
Through mapping/serilization maybe? (If looked it up, but didnt give me a solution)
Am I the only one who works with this kind of relations??

Thanks a lot for any help!




Aucun commentaire:

Enregistrer un commentaire