2020-03-25 12:51:26 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
2020-06-12 05:19:08 +00:00
|
|
|
import Watchable from './watchable';
|
2020-03-25 12:51:26 +00:00
|
|
|
|
2020-06-12 05:19:08 +00:00
|
|
|
export default class WatchableNamespaceIDs extends Watchable {
|
|
|
|
@service system;
|
2020-03-25 12:51:26 +00:00
|
|
|
|
|
|
|
findAll() {
|
|
|
|
const namespace = this.get('system.activeNamespace');
|
2020-06-12 05:19:08 +00:00
|
|
|
return super.findAll(...arguments).then(data => {
|
2020-03-25 12:51:26 +00:00
|
|
|
data.forEach(record => {
|
|
|
|
record.Namespace = namespace ? namespace.get('id') : 'default';
|
|
|
|
});
|
|
|
|
return data;
|
|
|
|
});
|
2020-06-12 05:19:08 +00:00
|
|
|
}
|
2020-03-25 12:51:26 +00:00
|
|
|
|
|
|
|
findRecord(store, type, id, snapshot) {
|
|
|
|
const [, namespace] = JSON.parse(id);
|
|
|
|
const namespaceQuery = namespace && namespace !== 'default' ? { namespace } : {};
|
|
|
|
|
2020-06-12 05:19:08 +00:00
|
|
|
return super.findRecord(store, type, id, snapshot, namespaceQuery);
|
|
|
|
}
|
2020-03-25 12:51:26 +00:00
|
|
|
|
|
|
|
urlForFindAll() {
|
2020-06-12 05:19:08 +00:00
|
|
|
const url = super.urlForFindAll(...arguments);
|
2020-03-25 12:51:26 +00:00
|
|
|
const namespace = this.get('system.activeNamespace.id');
|
|
|
|
return associateNamespace(url, namespace);
|
2020-06-12 05:19:08 +00:00
|
|
|
}
|
2020-03-25 12:51:26 +00:00
|
|
|
|
|
|
|
urlForQuery() {
|
2020-06-12 05:19:08 +00:00
|
|
|
const url = super.urlForQuery(...arguments);
|
2020-03-25 12:51:26 +00:00
|
|
|
const namespace = this.get('system.activeNamespace.id');
|
|
|
|
return associateNamespace(url, namespace);
|
2020-06-12 05:19:08 +00:00
|
|
|
}
|
2020-03-25 12:51:26 +00:00
|
|
|
|
|
|
|
urlForFindRecord(id, type, hash) {
|
|
|
|
const [name, namespace] = JSON.parse(id);
|
2020-06-12 05:19:08 +00:00
|
|
|
let url = super.urlForFindRecord(name, type, hash);
|
2020-03-25 12:51:26 +00:00
|
|
|
return associateNamespace(url, namespace);
|
2020-06-12 05:19:08 +00:00
|
|
|
}
|
2020-03-25 12:51:26 +00:00
|
|
|
|
|
|
|
xhrKey(url, method, options = {}) {
|
2020-06-12 05:19:08 +00:00
|
|
|
const plainKey = super.xhrKey(...arguments);
|
2020-03-25 12:51:26 +00:00
|
|
|
const namespace = options.data && options.data.namespace;
|
|
|
|
return associateNamespace(plainKey, namespace);
|
2020-06-12 05:19:08 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-25 12:51:26 +00:00
|
|
|
|
|
|
|
function associateNamespace(url, namespace) {
|
|
|
|
if (namespace && namespace !== 'default') {
|
|
|
|
url += `?namespace=${namespace}`;
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|