summaryrefslogtreecommitdiffstats
path: root/module/web/static/js/models/ConfigHolder.js
diff options
context:
space:
mode:
Diffstat (limited to 'module/web/static/js/models/ConfigHolder.js')
-rw-r--r--module/web/static/js/models/ConfigHolder.js67
1 files changed, 67 insertions, 0 deletions
diff --git a/module/web/static/js/models/ConfigHolder.js b/module/web/static/js/models/ConfigHolder.js
new file mode 100644
index 000000000..b05b1e14b
--- /dev/null
+++ b/module/web/static/js/models/ConfigHolder.js
@@ -0,0 +1,67 @@
+define(['jquery', 'backbone', 'underscore', 'app', './ConfigItem'],
+ function($, Backbone, _, App, ConfigItem) {
+
+ return Backbone.Model.extend({
+
+ defaults: {
+ name: "",
+ label: "",
+ description: "",
+ long_description: null,
+ // simple list but no collection
+ items: null,
+ info: null
+ },
+
+ // Model Constructor
+ initialize: function() {
+
+ },
+
+ // Loads it from server by name
+ fetch: function(options) {
+ options = App.apiRequest('loadConfig/"' + this.get('name') + '"', null, options);
+ return Backbone.Model.prototype.fetch.call(this, options);
+ },
+
+ save: function(options) {
+ var config = this.toJSON();
+ var items = [];
+ // Convert changed items to json
+ _.each(config.items, function(item) {
+ if (item.isChanged()) {
+ items.push(item.prepareSave());
+ }
+ });
+ config.items = items;
+ // TODO: only set new values on success
+
+ options = App.apiRequest('saveConfig', {config: config}, options);
+
+ return $.ajax(options);
+ },
+
+ parse: function(resp) {
+ // Create item models
+ resp.items = _.map(resp.items, function(item) {
+ return new ConfigItem(item);
+ });
+
+ return Backbone.Model.prototype.parse.call(this, resp);
+ },
+
+ isLoaded: function() {
+ return this.has('items') || this.has('long_description');
+ },
+
+ // check if any of the items has changes
+ hasChanges: function() {
+ var items = this.get('items');
+ if (!items) return false;
+ return _.reduce(items, function(a, b) {
+ return a || b.isChanged();
+ }, false);
+ }
+
+ });
+ }); \ No newline at end of file