Simple session data in Ember.js

I'm not sure how to store simple session data in an Ember.js application. As a toy project, I'm writing a Tic Tac Toe game, and I would like to store the player's nickname. In plain javascript, I'd store it in a cookie, but I guess it's not very "emberic".

Any tip about the best way to do this?

Upvotes: 1

Views: 1262

Answers (1)

Darshan Sawardekar
Darshan Sawardekar

Reputation: 5075

To persist state you have various options. With ember-data this is straightforward, you just change the adapter on your store.

For example to use LocalStorage apis to store models you can use,

App.Store = DS.Store.extend({
  revision: 12,
  adapter: DS.LSAdapter.create()
});

Now if you have a Player model in your app, when you save a record it will get automatically get saved in the LocalStorage.

The LSAdapter is a small library that adds this support.

Alternatively if aren't using ember-data you can directly persist to local storage on save and then load it in your model() hook on a Route. For simple key-value stuff I recommend using a library like lawnchair.

Upvotes: 1

Related Questions