[solved] Best practice for creating app default settings?

Node / Angular / Ionic noob here developing my first real app. I would like to create some default settings for this app (working with the Heroku Node example) and I’ve set up a settings page with a few switches and text fields. As of right now these fields are statically set in the tab-settings.html template.

The app is also using ngStorage.js for $localStorage access, which seems to work as a nice wrapper for the JS window.localStorage method.

I’d like to initialize the app with some default settings, but if the user changes anything on the settings tab then it should get stored in $localStorage and persist through a full app close and re-open. I’m just not quite sure where to define these default settings, whether it should be in a controller or a factory or… a .config() block?

Does anyone know of a decent way to manage default settings for an app? Many thanks for your time!

So far it looks like using angular.extend() in the angular.module.run() function is the way to go.

Attempted code:

angular.extend({
  accounts: [],
  cores: {},
  sparkData: {},
  settings: {
    sparkApiUrl: "https://api.spark.io/v1/"
  }
}, $localStorage);

I’m having some issues getting the $localStorage to merge on to the defaults properly - if I put $localStorage before the original object, then it saves properly and doesn’t get overwritten when the app restarts… but if I delete a localStorage variable and restart the app, that variable won’t restore.

Got it - http://ngmodules.org/modules/ngStorage has a $default method for specifying stock settings. MUCH easier than the deep merge and extend functions I was attempting!

angular.module('ionicBasicSpark', ['ionic', 'starter.controllers', 'starter.services'])

.run(function($ionicPlatform, $localStorage) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }

    // Load default settings and variables
    $localStorage.$default({
      'accounts': [],
      'cores': {},
      'sparkData': {},
      'settings': {
        sparkApiUrl: "https://api.spark.io/v1/"
      }
    });


  }); // .run

})

Localstorage can be cleaned by calling $localStorage.$reset() which dumps everything back to the defaults set in the code block above. Works well!