#77 - Allow claySettings to be modified from app.js (#150)

* .setSettings(key, value) to set a single setting
* .setSettings(object) to set multiple settings at once
This commit is contained in:
Yohan Robert
2016-10-21 10:36:55 -07:00
committed by Keegan Lillo
parent d18170337d
commit 46c0adb62c
2 changed files with 81 additions and 0 deletions
+33
View File
@@ -213,6 +213,39 @@ Clay.prototype.getSettings = function(response, convert) {
return convert === false ? settings : Clay.prepareSettingsForAppMessage(settings);
};
/**
* Updates the settings with the given value(s).
*
* @signature `clay.setSettings(key, value)`
* @param {String} key the property to set.
* @param {String} value the value assigned to _key_.
* @return {undefined}
*
* @signature `clay.setSettings(setting)`
* @param {Object} setting an object containing the key/value pairs to be set.
* @return {undefined}
*/
Clay.prototype.setSettings = function(key, value) {
var settingsStorage = {};
try {
settingsStorage = JSON.parse(localStorage.getItem('clay-settings')) || {};
} catch (e) {
console.error(e.toString());
}
if (typeof key === 'object') {
var settings = key;
Object.keys(settings).forEach(function(key) {
settingsStorage[key] = settings[key];
});
} else {
settingsStorage[key] = value;
}
localStorage.setItem('clay-settings', JSON.stringify(settingsStorage));
};
/**
* @param {string} input
* @param {string} [prefix='data:text/html;charset=utf-8,']