-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathwatchable-namespace-ids.js
56 lines (47 loc) · 1.62 KB
/
watchable-namespace-ids.js
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
import { inject as service } from '@ember/service';
import Watchable from './watchable';
import classic from 'ember-classic-decorator';
@classic
export default class WatchableNamespaceIDs extends Watchable {
@service system;
findAll() {
const namespace = this.get('system.activeNamespace');
return super.findAll(...arguments).then(data => {
data.forEach(record => {
record.Namespace = namespace ? namespace.get('id') : 'default';
});
return data;
});
}
findRecord(store, type, id, snapshot) {
const [, namespace] = JSON.parse(id);
const namespaceQuery = namespace && namespace !== 'default' ? { namespace } : {};
return super.findRecord(store, type, id, snapshot, namespaceQuery);
}
urlForFindAll() {
const url = super.urlForFindAll(...arguments);
const namespace = this.get('system.activeNamespace.id');
return associateNamespace(url, namespace);
}
urlForQuery() {
const url = super.urlForQuery(...arguments);
const namespace = this.get('system.activeNamespace.id');
return associateNamespace(url, namespace);
}
urlForFindRecord(id, type, hash) {
const [name, namespace] = JSON.parse(id);
let url = super.urlForFindRecord(name, type, hash);
return associateNamespace(url, namespace);
}
xhrKey(url, method, options = {}) {
const plainKey = super.xhrKey(...arguments);
const namespace = options.data && options.data.namespace;
return associateNamespace(plainKey, namespace);
}
}
function associateNamespace(url, namespace) {
if (namespace && namespace !== 'default') {
url += `?namespace=${namespace}`;
}
return url;
}