-
Notifications
You must be signed in to change notification settings - Fork 19
Singleton Resource
The default naming for resources uses a plural naming convention. However the JSON API Specification does support the use case for a singular name of a resource/endpoint.
How do you setup a singleton resource with this library?
Use options to make a request that identifies the resource as a singleton.
An example route for the me
resource:
export default Ember.Route.extend({
model() {
return store.find('me', { singleton: true });
}
});
The generated initializer for a resource uses pluralization by default, so manually change the pluralized names to singular (the same value passed to the generator).
ember g resource me
Then change the mes
to me
:
import Service from '../services/me';
import Model from '../models/me';
import Adapter from '../adapters/me';
import Serializer from '../serializers/me';
export function initialize(container, application) {
const adapter = 'service:me-adapter';
const serializer = 'service:me-serializer';
const service = 'service:me';
const model = 'model:me';
application.register(model, Model, { instantiate: false, singleton: false });
application.register(service, Service);
application.register(adapter, Adapter);
application.register(serializer, Serializer);
application.inject('service:store', 'me', service);
application.inject(service, 'serializer', serializer);
}
export default {
name: 'me-service',
after: 'store',
initialize: initialize
};
In the generated adapter be sure to use singular names to refer to the endpoint and resource type:
import ApplicationAdapter from './application';
import config from 'client/config/environment';
export default ApplicationAdapter.extend({
type: 'me',
url: config.APP.API_HOST + 'me'
});
There is some manual updates that are necessary to support a Singleton Resource which fits into the JSON API Specification. It would be nice to have an automated way to generate a resource as a plural or singular name, but the common use is plural. Singleton resources can be used, by using the option { singleton: true }
or { singleton: true, id: '111' }
when requesting a resource.