mirror of
https://github.com/pebble-dev/clay.git
synced 2026-08-27 20:36:33 -04:00
* .setSettings(key, value) to set a single setting * .setSettings(object) to set multiple settings at once
This commit is contained in:
committed by
Keegan Lillo
parent
d18170337d
commit
46c0adb62c
@@ -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,']
|
||||
|
||||
@@ -386,6 +386,54 @@ describe('Clay', function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('.setSettings', function() {
|
||||
it('it writes to localStorage when passing an object',
|
||||
function() {
|
||||
var clay = fixture.clay([]);
|
||||
var settings = {
|
||||
key1: 'value1',
|
||||
key2: 'value2'
|
||||
};
|
||||
var expected = {
|
||||
key1: 'value1',
|
||||
key2: 'value2'
|
||||
};
|
||||
|
||||
clay.setSettings(settings);
|
||||
assert.equal(
|
||||
localStorage.getItem('clay-settings'),
|
||||
JSON.stringify(expected)
|
||||
);
|
||||
});
|
||||
|
||||
it('it writes to localStorage when passing a key and a value',
|
||||
function() {
|
||||
var clay = fixture.clay([]);
|
||||
var expected = {
|
||||
key1: 'value1',
|
||||
key2: 'value2%7Dbreaks'
|
||||
};
|
||||
|
||||
clay.setSettings('key1', 'value1');
|
||||
clay.setSettings('key2', 'value2%7Dbreaks');
|
||||
assert.equal(localStorage.getItem('clay-settings'),
|
||||
JSON.stringify(expected)
|
||||
);
|
||||
});
|
||||
|
||||
it('doesn\'t throw and logs an error if settings in localStorage are broken',
|
||||
function() {
|
||||
var clay = fixture.clay([]);
|
||||
var errorStub = sinon.stub(console, 'error');
|
||||
localStorage.setItem('clay-settings', 'not valid JSON');
|
||||
assert.doesNotThrow(function() {
|
||||
clay.setSettings('key', 'value');
|
||||
});
|
||||
assert(errorStub.calledWithMatch(/SyntaxError/i));
|
||||
errorStub.restore();
|
||||
});
|
||||
});
|
||||
|
||||
describe('.meta', function() {
|
||||
var emptyMeta = {
|
||||
activeWatchInfo: null,
|
||||
|
||||
Reference in New Issue
Block a user