Ember service based singleton pattern (use with ember-data!)
run the generator
ember generate singleton chatroom
This will create a models/chatroom.js
, services/chatroom.js
Declare your model/chatroom.js
as your would any run-of-the-mill regular DS.Model (notice SingleModel
is mixed in for you):
import DS from 'ember-data';
import { SingleModel } from 'ember-single';
const { attr, hasMany } = DS;
const Chatroom = DS.Model.extend(SingleModel, {
topic: attr('string'),
messages: hasMany('message),
users: hasMany('user')
});
Your services/chatroom.js
becomes the entry point to your singleton:
Ember.Service.extend(SingleService, {
modelFor: 'chatroom',
modelPromise: computed(function() {
// setup your model here
// this is a computed function necessarily returns a promise
// default implementation is as follows:
return this.store.findRecord(this.get('modelFor'));
})
});
git clone
this repositorynpm install
bower install
ember serve
- Visit your app at http://localhost:4200.
npm test
(Runsember try:testall
to test your addon against multiple Ember versions)ember test
ember test --server
ember build
For more information on using ember-cli, visit http://ember-cli.com/.