From 4e9319f2f932a3baf7a8c9c0548eafe8647238f2 Mon Sep 17 00:00:00 2001 From: RaNaN Date: Fri, 16 Aug 2013 19:59:25 +0200 Subject: settings for individual accounts --- pyload/web/app/scripts/models/Account.js | 21 +++++++++++++++++++-- pyload/web/app/scripts/models/ConfigItem.js | 13 ++++++++----- 2 files changed, 27 insertions(+), 7 deletions(-) (limited to 'pyload/web/app/scripts/models') diff --git a/pyload/web/app/scripts/models/Account.js b/pyload/web/app/scripts/models/Account.js index d98d3374f..94893f3e2 100644 --- a/pyload/web/app/scripts/models/Account.js +++ b/pyload/web/app/scripts/models/Account.js @@ -1,4 +1,4 @@ -define(['jquery', 'backbone', 'underscore', 'app', 'utils/apitypes'], function($, Backbone, _, App, Api) { +define(['jquery', 'backbone', 'underscore', 'app', 'utils/apitypes', './ConfigItem'], function($, Backbone, _, App, Api, ConfigItem) { 'use strict'; return Backbone.Model.extend({ @@ -30,6 +30,15 @@ define(['jquery', 'backbone', 'underscore', 'app', 'utils/apitypes'], function($ }, + parse: function(resp) { + // Convert config to models + resp.config = _.map(resp.config, function(item) { + return new ConfigItem(item); + }); + + return resp; + }, + fetch: function(options) { var refresh = _.has(options, 'refresh') && options.refresh; options = App.apiRequest('getAccountInfo', @@ -47,8 +56,16 @@ define(['jquery', 'backbone', 'underscore', 'app', 'utils/apitypes'], function($ }, save: function() { + // use changed config items only + var data = this.toJSON(); + data.config = _.map(_.filter(data.config, function(c){ + return c.isChanged(); + }), function(c) { + return c.prepareSave(); + }); + // On success wait 1sec and trigger event to reload info - var options = App.apiRequest('updateAccountInfo', {account: this.toJSON()}, { + var options = App.apiRequest('updateAccountInfo', {account: data}, { success: function() { _.delay(function() { App.vent.trigger('account:updated'); diff --git a/pyload/web/app/scripts/models/ConfigItem.js b/pyload/web/app/scripts/models/ConfigItem.js index ecb44cbbc..8c75f45f6 100644 --- a/pyload/web/app/scripts/models/ConfigItem.js +++ b/pyload/web/app/scripts/models/ConfigItem.js @@ -4,6 +4,8 @@ define(['jquery', 'backbone', 'underscore', 'app', 'utils/apitypes'], return Backbone.Model.extend({ + idAttribute: 'name', + defaults: { name: '', label: '', @@ -29,11 +31,12 @@ define(['jquery', 'backbone', 'underscore', 'app', 'utils/apitypes'], if (this.get('inputView')) this.set('value', this.get('inputView').getVal()); - var data = this.toJSON(); - delete data.inputView; - delete data.description; - - return data; + // These values are enough to be handled correctly + return { + name: this.get('name'), + value: this.get('value'), + '@class': this.get('@class') + }; } }); }); \ No newline at end of file -- cgit v1.2.3