Reputation: 3829
I have foo
an instance of the ember-data model thing
. thing.js has the following property :
owner: DS.belongsTo('user')
If I have foo
with an empty owner, how can I, with only foo
and the 'owner'
string, retrieve the value 'user'
representing the model of the owner relation?
EDIT: I want to allow my select-relation component to works with relations where the name is different from the class name
Upvotes: 1
Views: 135
Reputation: 1074
This looks like a use case for typeForRelationship.
In your example you should be able to do something like
store.modelFor('thing').typeForRelationship('owner', store);
If you don't like that approach you can use the belongsTo reference API, where you use the meta data from the relationship to get the type
foo.belongsTo('owner').type
The only thing with that approach is that the type
property may not be public API and possible (though unlikely) to change at some point.
Upvotes: 1
Reputation: 3829
It seems I can do the following :
this.get('model').get('_internalModel._relationships.initializedRelationships.'+this.get('relation')+'.relationshipMeta.type')
model
being an instance and relation
the string of the relation name, it correctly return the model of the relation.
EDIT : a better solution not using private API courtesy from the ember discord :
function getRelatedModelName(record, relationName){
let ParentModelClass = record.constructor;
let meta = get(ParentModelClass, 'relationshipsByName').get(relationName);
return meta.type;
}
Upvotes: 0
Reputation: 2276
It sounds like you have some work to do to finish setting up your relationships. Have a read through this page of the guides.
If the relationships are set up correctly, to get the associated user, you should be able to do foo.owner
. This assumes that users are already present in the store. I recommend using the Ember Inspector browser plugin to debug the relationships.
Upvotes: 1