open-vault/ui/app/macros/maybe-query-record.js
2018-10-18 14:55:50 -05:00

18 lines
593 B
JavaScript

import { computed } from '@ember/object';
import ObjectProxy from '@ember/object/proxy';
import PromiseProxyMixin from '@ember/object/promise-proxy-mixin';
import { resolve } from 'rsvp';
export function maybeQueryRecord(modelName, options = {}, ...keys) {
return computed(...keys, {
get() {
const query = typeof options === 'function' ? options(this) : options;
const PromiseObject = ObjectProxy.extend(PromiseProxyMixin);
return PromiseObject.create({
promise: query ? this.get('store').queryRecord(modelName, query) : resolve({}),
});
},
});
}