This repository was archived by the owner on Jan 22, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathrelated-proxy.js
More file actions
188 lines (167 loc) · 4.8 KB
/
Copy pathrelated-proxy.js
File metadata and controls
188 lines (167 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
@module ember-jsonapi-resources
@submodule utils
@main RelatedProxyUtil
**/
import Ember from 'ember';
import RSVP from 'rsvp';
import { pluralize } from 'ember-inflector';
/**
Utility for creating promise proxy objects for related resources
@class RelatedProxyUtil
@static
@final
*/
const RelatedProxyUtil = Ember.Object.extend({
/**
Checks for required `relationship` property
@method init
*/
init: function () {
this._super();
if (typeof this.get('relationship') !== 'string') {
throw new Error('RelatedProxyUtil#init expects `relationship` property to exist.');
}
return this;
},
/**
The name of the relationship
@property resource
@type String
@required
*/
relationship: null,
/**
The name of the type of resource
@property type
@type String
@required
*/
type: null,
/**
Proxy for the requested relation, resolves w/ content from fulfilled promise
@method createProxy
@param {Resource} resource
@param {String} kind 'toMany' or 'toOne'
@return {PromiseProxy|ObjectProxy|ArrayProxy} proxy instance, new resource uses mock relations
*/
createProxy(resource, kind) {
let proxyFactory, newContent;
if (kind === 'toMany') {
proxyFactory = Ember.ArrayProxy;
newContent = Ember.A([]);
} else if (kind === 'toOne') {
proxyFactory = Ember.ObjectProxy;
newContent = null;
}
if (resource.get('isNew')) {
return proxyFactory.create({ content: newContent });
} else {
let proxy = this.proxySetup(resource, kind, proxyFactory);
return this.proxyResolution(resource, proxy);
}
},
/**
@method proxySetup
@param {Resource} resource
@param {String} kind 'toMany' or 'toOne'
@param {Ember.ObjectProxy|Ember.ArrayProxy} proxyFactory
@return {PromiseProxy} proxy
*/
proxySetup(resource, kind, proxyFactory) {
let relation = this.get('relationship');
let type = this.get('type');
let url = this.proxyUrl(resource, relation);
let owner = Ember.getOwner(resource);
let service = owner.lookup('service:' + pluralize(type));
let promise = this.promiseFromCache(resource, relation, service);
promise = promise || service.findRelated({'relation': relation, 'type': type}, url);
let proxyProto = proxyFactory.extend(Ember.PromiseProxyMixin, {
'promise': promise, 'type': relation, 'kind': kind
});
return proxyProto.create({
content: (kind === 'toOne') ? null : Ember.A([])
});
},
/**
@method proxyResolution
@param {proxy} resource
@return {PromiseProxy} proxy
*/
proxyResolution(resource, proxy) {
proxy.then(
function (resources) {
proxy.set('content', resources);
let relation = proxy.get('type');
let kind = proxy.get('kind');
resource.didResolveProxyRelation(relation, kind, resources);
return resources;
},
function (error) {
Ember.Logger.error(error);
throw error;
}
);
return proxy;
},
/**
Proxy url to fetch for the resource's relation
@method proxyUrl
@param {Resource} resource
@param {String} relation
@return {PromiseProxy} proxy
*/
proxyUrl(resource, relation) {
let related = linksPath(relation);
let url = resource.get(related);
if (typeof url !== 'string') {
throw new Error('RelatedProxyUtil#_proxyUrl expects `model.'+ related +'` property to exist.');
}
return url;
},
/**
Lookup relation from service cache and pomisify result
@method promiseFromCache
@param {Resource} resource
@param {String} relation
@param {Object} service
@return {Promise|null}
*/
promiseFromCache(resource, relation, service) {
let data = resource.get('relationships.' + relation + '.data');
if (!data) { return; }
let content, found;
if (Array.isArray(data)) {
content = Ember.A([]);
for (let i = 0; i < data.length; i++) {
found = this.serviceCacheLookup(service, data[i]);
if (found) {
content.push(found);
}
}
content = (data.length && data.length === content.length) ? content : null;
} else {
content = this.serviceCacheLookup(service, data);
}
return (content) ? RSVP.Promise.resolve(content) : null;
},
/**
Lookup data in service cache
@method serviceCacheLookup
@param {Object} service
@param {Object} data
@return {Resource|undefined}
*/
serviceCacheLookup(service, data) {
return (typeof data === 'object' && data.id) ? service.cacheLookup(data.id) : undefined;
}
});
export default RelatedProxyUtil;
/**
@method linksPath
@param {String} relation
@return {String} path to the related link
*/
export function linksPath(relation) {
return ['relationships', relation, 'links', 'related'].join('.');
}