From 3e2b574ee4e354c5d8f93ea7a0db690988d8877c Mon Sep 17 00:00:00 2001 From: Karel Kremer Date: Wed, 19 Aug 2015 15:55:07 +0200 Subject: [PATCH 1/3] added type parameter to the relations The type parameter is necessary to choose the service build the right resources. The type is not always the same as the relation name. --- addon/models/resource.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/addon/models/resource.js b/addon/models/resource.js index 56ece5c..b4cb3f1 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,21 @@ 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 {String} type */ -export function hasOne(relation) { +export function hasOne(relation, type) { assertDasherizedHasOneRelation(relation); const util = RelatedProxyUtil.create({'relationship': relation}); const path = linksPath(relation); + type = (type ? type : relation) 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: relation, type: type, kind: 'hasOne'}); } function assertDasherizedHasOneRelation(name) { @@ -498,18 +501,21 @@ 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} type */ -export function hasMany(relation) { +export function hasMany(relation, type) { assertDasherizedHasManyRelation(relation); const util = RelatedProxyUtil.create({'relationship': relation}); const path = linksPath(relation); + type = (type ? type : relation) 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: relation, type: type, kind: 'hasMany'}); } function assertDasherizedHasManyRelation(name) { From da0337d4f84bdc6a70a41bfb1987d39dc3c267ca Mon Sep 17 00:00:00 2001 From: Karel Kremer Date: Wed, 19 Aug 2015 18:24:13 +0200 Subject: [PATCH 2/3] fix missing semicolons... --- addon/models/resource.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/addon/models/resource.js b/addon/models/resource.js index b4cb3f1..7cd23d0 100644 --- a/addon/models/resource.js +++ b/addon/models/resource.js @@ -483,7 +483,7 @@ export function hasOne(relation, type) { assertDasherizedHasOneRelation(relation); const util = RelatedProxyUtil.create({'relationship': relation}); const path = linksPath(relation); - type = (type ? type : relation) + type = (type ? type : relation); return Ember.computed(path, function () { return util.createProxy(this, type, Ember.ObjectProxy); }).meta({relation: relation, type: type, kind: 'hasOne'}); @@ -512,7 +512,7 @@ export function hasMany(relation, type) { assertDasherizedHasManyRelation(relation); const util = RelatedProxyUtil.create({'relationship': relation}); const path = linksPath(relation); - type = (type ? type : relation) + type = (type ? type : relation); return Ember.computed(path, function () { return util.createProxy(this, type, Ember.ArrayProxy); }).meta({relation: relation, type: type, kind: 'hasMany'}); From 664e69faeeb38bd0e367943629ed80b3974babca Mon Sep 17 00:00:00 2001 From: Karel Kremer Date: Fri, 21 Aug 2015 09:28:58 +0200 Subject: [PATCH 3/3] Updated the format of relationships the relations now accept either a string or an object. the object holds the specification of the relationship in the format: hasMany( { resource: "friends", type: "persons" } ) If the type of the relationship is the same as the name of the relationship, type can be omitted in the specification object. Passing hasMany( "persons" ) is the same as hasMany( { resource: "persons", type: "persons" } ) --- addon/models/resource.js | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/addon/models/resource.js b/addon/models/resource.js index 7cd23d0..e9c70f7 100644 --- a/addon/models/resource.js +++ b/addon/models/resource.js @@ -476,17 +476,20 @@ function linksPath(relation) { type is optional and defaults to relation @method hasOne - @param {String} relation - @param {String} type + @param {Object|String} relation */ -export function hasOne(relation, type) { - assertDasherizedHasOneRelation(relation); - const util = RelatedProxyUtil.create({'relationship': relation}); - const path = linksPath(relation); - type = (type ? type : relation); +export function hasOne(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, type, Ember.ObjectProxy); - }).meta({relation: relation, type: type, kind: 'hasOne'}); + }).meta({relation: relationName, type: type, kind: 'hasOne'}); } function assertDasherizedHasOneRelation(name) { @@ -505,17 +508,20 @@ function assertDasherizedHasOneRelation(name) { type is optional and defaults to relation @method hasMany - @param {String} relation - @param {String} type + @param {String|Object} relation */ -export function hasMany(relation, type) { - assertDasherizedHasManyRelation(relation); - const util = RelatedProxyUtil.create({'relationship': relation}); - const path = linksPath(relation); - type = (type ? type : relation); +export function hasMany(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, type, Ember.ArrayProxy); - }).meta({relation: relation, type: type, kind: 'hasMany'}); + }).meta({relation: relationName, type: type, kind: 'hasMany'}); } function assertDasherizedHasManyRelation(name) {