Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions addon/models/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,12 @@ const RelatedProxyUtil = Ember.Object.extend({
@param {Ember.ObjectProxy|Ember.ArrayProxy} proxyFactory
@return {PromiseProxy} proxy
*/
createProxy: function (resource, proxyFactory) {
createProxy: function (resource, type, proxyFactory) {
const relation = this.get('relationship');
const url = this.proxyUrl(resource, relation);
const service = resource.container.lookup('service:' + pluralize(relation));
const service = resource.container.lookup('service:' + pluralize(type));
let promise = this.promiseFromCache(resource, relation, service);
promise = promise || service.findRelated(relation, url);
promise = promise || service.findRelated(type, url);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API returned a URL for the relation which is still used for the url when calling service.findRelated; however, it seems that the service you want to use should be configurable based on either the (default) resource name or an optional type name

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is already the case: the type defines the service to use in line

const service = resource.container.lookup('service:' + pluralize(type));

Resource defines the url to use:

const relation = this.get('relationship');
const url = this.proxyUrl(resource, relation);

So the url is created based on the this.get('relationship'), which gets the value of resource.
The type is passed in and is either passed to the relation (e.g. hasOne) in a hash or takes the same value as resource

...

The only problem I have now is that I probably should have passed the type like the relationship is passed: in the create, not as a parameter

const util = RelatedProxyUtil.create({'relationship': relationName});

would become

const util = RelatedProxyUtil.create({'relationship': relationName, 'type': type});

and would probably be more clear...

let proxy = proxyFactory.extend(Ember.PromiseProxyMixin, {
'promise': promise, 'type': relation
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious if 'promise': promise, 'type': relation would also need to change to 'promise': promise, 'type': type ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I can tell, the type parameter on line 398 is not used at the moment, but I am probably missing something here...

});
Expand Down Expand Up @@ -472,18 +472,24 @@ function linksPath(relation) {
}

/**
Helper to setup a has one relationship to another resource
Helper to setup a has one relationship to another resource,
type is optional and defaults to relation

@method hasOne
@param {String} relation
@param {Object|String} relation
*/
export function hasOne(relation) {
assertDasherizedHasOneRelation(relation);
const util = RelatedProxyUtil.create({'relationship': relation});
const path = linksPath(relation);
let relationName = relation;
if (typeof relation !== 'string') {
relationName = relation.resource;
}
const util = RelatedProxyUtil.create({'relationship': relationName});
const path = linksPath(relationName);
assertDasherizedHasOneRelation(relationName);
const type = (relation.type ? relation.type : relationName);
return Ember.computed(path, function () {
return util.createProxy(this, Ember.ObjectProxy);
}).meta({relation: relation, kind: 'hasOne'});
return util.createProxy(this, type, Ember.ObjectProxy);
}).meta({relation: relationName, type: type, kind: 'hasOne'});
}

function assertDasherizedHasOneRelation(name) {
Expand All @@ -498,18 +504,24 @@ function assertDasherizedHasOneRelation(name) {
}

/**
Helper to setup a has many relationship to another resource
Helper to setup a has many relationship to another resource,
type is optional and defaults to relation

@method hasMany
@param {String} relation
@param {String|Object} relation
*/
export function hasMany(relation) {
assertDasherizedHasManyRelation(relation);
const util = RelatedProxyUtil.create({'relationship': relation});
const path = linksPath(relation);
let relationName = relation;
if (typeof relation !== 'string') {
relationName = relation.resource;
}
const util = RelatedProxyUtil.create({'relationship': relationName});
const path = linksPath(relationName);
assertDasherizedHasManyRelation(relationName);
const type = (relation.type ? relation.type : relationName);
return Ember.computed(path, function () {
return util.createProxy(this, Ember.ArrayProxy);
}).meta({relation: relation, kind: 'hasMany'});
return util.createProxy(this, type, Ember.ArrayProxy);
}).meta({relation: relationName, type: type, kind: 'hasMany'});
}

function assertDasherizedHasManyRelation(name) {
Expand Down