Using different stores in the same app


#1

I would like to use the FixturesAdapter for some of the models and a LocalStorageAdapter for other models. So far I have only seen the storage adapter being set on a per app basis, not per model as I would need it in this case.

On a related note, one of the models I would like to store in local storage needs to be a singleton, i.e. only one instance may exists. Does Ember and/or Ember Data provide any functionality towards this? Or do I treat the model just like a collection and manually make sure only one can be created and only the first can be “found”?

Many thanks in advance!


#2

You can define a per model adapter by naming it after your model.

For example:

window.App = Ember.Application.create();
App.ApplicationAdapter = DS.LSAdapter.extend();
App.PostAdapter = DS.FixtureAdapter.extend();


// Post will use the PostAdapter (fixtures)
App.Post = DS.Model.extend();

// Comment will use the default ApplicationAdapter (localstorage)
App.Comment = DS.Model.extend();

Update:

One way to set up a singleton sort of model is write an initializer to create it and inject it’s value into the parts of the application where you will need it. I think this stack overflow answer explains it pretty well.