0
votes

I use Ember in combination with Electron.

As the user can define some settings for the app, I thought it was a elegant idea to store the settings data in local storage.

For that I use ember-local-storage adapter.

Storing, deleting and displaying this data works like a charm BUT These settings should be used in some functions, so I want to get them out of the local storage. I expected an array or JSON object- or even the same format as it is shown in local storage. But I only receive a huge abstract object ?!

This is what I want to store (e.g.):

{"identifier":"identifier1","bid":"6653ggd245","name":"test1"}
{"identifier":"identifier2","bid":"kkhd7765vv","name":"test2"}

This is what is displayed in localstorage (dev tools):

{"buttons":
  {"records":
    {"i7lvj":{"id":"i7lvj","identifier":"identifier1","bid":"6653ggd245","name":"test1"},"i80vm":{"id":"i80vm","identifier":"identifier2","bid":"kkhd7765vv","name":"test2"}
  }}}

This is how I tried to access the data:

this.get('store').findAll('buttons').then(function(SavButtons){

      console.log(SavButtons);
 });// gett all stored buttons


this.get('store').findRecord('buttons','i7lvj').then(function(SavButtons){

      console.log(SavButtons.data);
 });// get specific button -> works 

This data is the base to generate an params array to use for Promises for API requests. What can I do to get this in a reusable structure ? For example: 0:{record1} 1:{record2}

Or is there even a simpler/better way to store settings made by the user after the app is closed, maybe I am missing something.

Thanks a lot!

1
You can try toArray method to convert into plain array. or use forEach to traverse and build the required structure and store it using local storage adapter and retrieve it.Ember Freak

1 Answers

0
votes

First, are you sure that your model called "buttons"? You probably need to use

this.get('store').findAll('button')

Also, what you receive from store is Ember.ArrayProxy or some other class extended from Ember.Array. It has special methods to work with elements, you should try those

Second, I don't think that Ember Data is good for settings. I find it much easier to create custom "settings" service and use that. Let me share my implementation that I use with node-webkit. It will not work for you "as is", but you might get an idea. I made it with support for both files and local storage (files for nw-app and storage for web-app), you can simplify _persist and _restore methods if you need just local storage.

Brief explanation of how it works: it's a basic service with modified init, set and setProperties methods. So when you call in your controller this.get('settings').set('key', vale);, settings will be persisted in local storage. And when service is initialized, settings will be loaded from storage to memory. One limitation: you must explicitly call set and setProperties methods of settings service, if you will use something like this.set('settings.key', value);, settings will not be persisted. The main advantage of separate service over ember data is that you don't need to create models for different types of settings, you can save any simple value or POJO.