Skip to content

app preferences API

jhugman edited this page May 25, 2012 · 2 revisions

A persistent key value storage for scalar values.

This requires a module called Environment to be declared somewhere in your app.

Usage

Making changes

var preferences = require("app-preferences");

preferences.put("latestSuccessfulCheckedAt", Date.now());
preferences.put("latestIdRead", lastId);
preferences.remove("lastError");

// go to native
preferences.commit();

Getting values

var preferences = require("app-preferences");
var url = preferences.get("serverUrl");

Public methods:

  • preferences.get(string key...). Returns a value associated with that key. If more than one key is supplied, then the keys will be tried in succession. The first non-undefined value will be returned, or undefined if nothing was found.
  • preferences.put(string key, any value). The value needs to be simple type that can be easily serialized – i.e. numbers, strings, booleans. Changes need to be committed to become persistent.
  • preferences.remove(key). Remove the key from store. Changes need to be committed to become persistent.

Hints

Build time file selection may be used to good effect to create configurations dependent on built type. e.g.:

  • Environment.dev.js
  • Environment.qa.js
  • Environment.prod.js
  • Environment.android.prod.js

meta

extension name: "app-preferences"
synonyms: "Settings"
implemented in: kirin-core
api-status: mature
implementations: android, ios

Implementation interface

module.exports = {
    namespace: "com.futureplatforms.kirin.extensions.preferences",
    classes: {
        "Settings": {
            implementedBy: "javascript",
            docs: "mergeOrOverwrite and resetEnviroment is called at onLoad() time",
            methods: {
                mergeOrOverwrite: [{latestNativePreferences : "object"}],
                resetEnvironment: []
            }
        },
        "SettingsBackend": {
            implementedBy: "native",
            docs: "Called in response to a settings.commit()",
            methods: {
                "writeStoreWithContents:andDeletes:": [{contents: "object"}, {deletes: "array"}]
            }
        }
    }
};