diff --git a/addon/models/resource.js b/addon/models/resource.js index 56ece5c..e9c70f7 100644 --- a/addon/models/resource.js +++ b/addon/models/resource.js @@ -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); let proxy = proxyFactory.extend(Ember.PromiseProxyMixin, { 'promise': promise, 'type': relation }); @@ -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) { @@ -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) {