Skip to content

Instantly share code, notes, and snippets.

@applehat
Last active December 11, 2015 06:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save applehat/4559471 to your computer and use it in GitHub Desktop.
Save applehat/4559471 to your computer and use it in GitHub Desktop.
Alloy.Globals.settings = {}; // Create Empty Object.
/* Settings Getter - Can also just access Alloy.Globals.settings[key] */
Alloy.Globals.settings.get = function(key) {
return Alloy.Globals.settings[key] || null;
}
/* Settings Saver */
Alloy.Globals.settings.set = function(key,value) {
var settings = Alloy.createCollection('settings');
settings.fetch();
var current = settings.where({key: key}); // find setting with key.
if (current.length)
{
for (x in current)
{
var model = settings.get(current[x].id); // remove any current settings with this key..
model.destroy();
}
console.log('[settings] Setting Updated '+key+' = '+value + " (overwriting old value)");
}
else
{
console.log('[settings] Setting Updated '+key+' = '+value);
}
var setting = Alloy.createModel('settings', {key:key,value:value});
settings.add(setting);
setting.save();
Alloy.Globals.settings[key] = value;
}
/* On first load, populated settings */
for (x in Alloy.CFG.settings) {
Alloy.Globals.settings[x] = Alloy.CFG.settings[x];
console.log('[settings] Loading setting from config.json');
} // Load config.json
var settings = Alloy.createCollection('settings');
settings.fetch();
settings.each(function(model){
Alloy.Globals.settings[model.get('key')] = model.get('value');
console.log('[settings] Loading setting from collection');
}); // Load Settings Table. Override if exists.
/* Debug */
console.log("[boot] Settings Loaded: "+JSON.stringify(Alloy.Globals.settings));
exports.definition = {
config: {
"columns": {
"key":"string",
"value":"string"
},
"adapter": {
"type": "sql",
"collection_name": "settings"
}
},
extendModel: function(Model) {
_.extend(Model.prototype, {
}); // end extend
return Model;
},
extendCollection: function(Collection) {
_.extend(Collection.prototype, {
}); // end extend
return Collection;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment