-
-
Save saggy/5620658 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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