hashg
hashg

Reputation: 806

Build dynamic relationship in ember data

I have access to API endpoints to only get, cannot suggest or apply any modifications.

This a sample schema of each api:

var person = DS.Model.extend {
    firstName: DS.attr(),
    lastName: DS.attr()
}

var credit = DS.Model.extend {
    name: DS.attr(),
    personId: : DS.belongsTo('person')
}

var debit = DS.Model.extend {
    name: DS.attr(),
    personId: : DS.belongsTo('person')
}

I can get api/person, api/credit, api/debit individually. After fetching the data. I want to map relationship between person and credit/debit similar to this...

var person = DS.Model.extend {
    firstName: DS.attr(),
    lastName: DS.attr(),
    **debits: DS.hasMany('debit'),**
    **credits: DS.hasMany('credit')**
}

How can I accomplish this in ember-data?

JSbin - http://emberjs.jsbin.com/gewowucamo/8/edit?html,js,output

Upvotes: 0

Views: 258

Answers (2)

jmurphyau
jmurphyau

Reputation: 2309

Everything you're doing will work. When you load the data using store.push() it builds the related relationships for you (if they exist).

This JSBin gives you an example of what you're doing that works.

I believe the problem you have is likely related to using Fixtures - and at a guess I would assume it doesn't follow the same behaviour/logic when loading data.

Fixtures are being removed from Ember (see this PR)

Upvotes: 1

Paul
Paul

Reputation: 1220

You have already answered your own questions, as the code you have given should already work.

Upvotes: 0

Related Questions