This class allows applications to store and retrieve user and system preference and configuration data.
This data is stored persistently in an implementation-dependent backing store.
Typical implementations include flat files, OS-specific registries, directory servers and SQL databases.
The user of this class doesn't need to be concerned with details of the backing store.
1 Preferences
Each instance of this class represents a node in a hierarchical collection of preference data. If the node called does not exist, it will be created.
Copy
let prefs = new Ax.util.Preferences("userPreferences");
2 Setting preferences
Prefernces are stored as key-value pairs. Values can be Integers
, Booleans
, Doubles
or Strings
, although they will always be stored as Strings
. That is why there are getters and setters for each type.
Copy
let prefs = new Ax.util.Preferences("userPreferences"); // Set preferences for the node prefs.setInt('int', 1); prefs.setBoolean('boolean', true); prefs.setDouble('double', 1.0); prefs.setString('string', '1');
3 Getting preferences
Preferences are retrieved by their key. As mentioned before, all preference values are stored as Strings
, so a getter for each type is required. Every getter has 2 parameters:
- The key associated to the value we want.
- A default value, that is, the value that will be returned if the key has no associated value or of the value cannot be converted to the getter type.
Copy
let prefs = new Ax.util.Preferences("newNode"); // Set preferences for the node prefs.setInt('int', 1); prefs.setBoolean('boolean', true); prefs.setDouble('double', 1.0); prefs.setString('string', 'A'); // Get node preferences console.log(prefs.getInt('int', 0)); // 1 console.log(prefs.getBoolean('boolean', false)); // true console.log(prefs.getDouble('double', 0.0)); // 1.0 console.log(prefs.getString('string', 'B')); // A // Get default values console.log(prefs.getInt('double', 0)); // 0 console.log(prefs.getString('wrongKey', 'B')); // B
1
true
1.0
A
0
B