From 1af28bc2ac7ec2fd5701d92e0e3254eda852d64f Mon Sep 17 00:00:00 2001 From: Ricardo Tomasi Date: Thu, 23 May 2013 10:57:37 -0300 Subject: [PATCH] Working settings --- core/admin/assets/js/settings.js | 38 ++++++++++++-------- core/admin/controllers/index.js | 14 +++++--- core/admin/views/settings.hbs | 5 +-- core/shared/api.js | 8 ++--- core/shared/models/dataProvider.bookshelf.js | 21 +++++------ 5 files changed, 49 insertions(+), 37 deletions(-) diff --git a/core/admin/assets/js/settings.js b/core/admin/assets/js/settings.js index 98fec36207..2152915afa 100644 --- a/core/admin/assets/js/settings.js +++ b/core/admin/assets/js/settings.js @@ -13,6 +13,18 @@ $('.settings-content').fadeOut().delay(250); $(newPage).fadeIn(); + }, + + defaultSettings = { + title: 'My Blog', + description: '' + }, + + getSettings = function () { + return $.extend(defaultSettings, { + title : $('#blog-title').val(), + description : $('#blog-description').val() + }); }; $(document).ready(function () { @@ -31,22 +43,18 @@ checkboxClass: 'icheckbox_square-grey' }); - $('.button-save').click(function(e){ + $('.button-save').click(function (e) { e.preventDefault(); - console.log(getSettings()); - }) + var data = getSettings(); + $.ajax({ + method: 'PUT', + url: '/api/v0.1/settings/edit', + data: data, + success: function (res, xhr, c) { + console.log(xhr, c); + } + }); + }); }); - var defaultSettings = { - title: 'My Blog', - description: '' - }; - - function getSettings () { - return $.extend(defaultSettings, { - title : $('#blog-title').val(), - description : $('#blog-description').val() - }); - } - }(jQuery)); \ No newline at end of file diff --git a/core/admin/controllers/index.js b/core/admin/controllers/index.js index 9f8aec81b9..9182951679 100644 --- a/core/admin/controllers/index.js +++ b/core/admin/controllers/index.js @@ -131,10 +131,16 @@ }); }, 'settings': function (req, res) { - res.render('settings', { - bodyClass: 'settings', - adminNav: setSelected(adminNavbar, 'settings') - }); + api.settings.browse() + .then(function (settings) { + settings = settings.toJSON(); + settings = _.object(_.pluck(settings, 'key'), _.pluck(settings, 'value')); + res.render('settings', { + bodyClass: 'settings', + adminNav: setSelected(adminNavbar, 'settings'), + settings: settings + }); + }); }, 'debug': { /* ugly temporary stuff for managing the app before it's properly finished */ index: function (req, res) { diff --git a/core/admin/views/settings.hbs b/core/admin/views/settings.hbs index 3124d827d5..8ecdbf52b3 100644 --- a/core/admin/views/settings.hbs +++ b/core/admin/views/settings.hbs @@ -27,11 +27,12 @@
+ {{#with settings}}
@@ -81,7 +82,7 @@
- + {{/with}}
diff --git a/core/shared/api.js b/core/shared/api.js index 61f48dc0dc..db32e6f835 100644 --- a/core/shared/api.js +++ b/core/shared/api.js @@ -63,11 +63,11 @@ browse: function (options) { return when.call(ghost.dataProvider().settings.browse, options); }, - read: function (key) { - return when.call(ghost.dataProvider().settings.read, key); + read: function (options) { + return when.call(ghost.dataProvider().settings.read, options.key); }, - edit: function (key, value) { - return when.call(ghost.dataProvider().settings.edit, key, value); + edit: function (options) { + return when.call(ghost.dataProvider().settings.edit, options); } }; diff --git a/core/shared/models/dataProvider.bookshelf.js b/core/shared/models/dataProvider.bookshelf.js index 3ec16a7d61..5abc72d18e 100644 --- a/core/shared/models/dataProvider.bookshelf.js +++ b/core/shared/models/dataProvider.bookshelf.js @@ -10,6 +10,7 @@ models = require('./models'), bcrypt = require('bcrypt'), when = require("when"), + _ = require("underscore"), DataProvider, instance; @@ -128,27 +129,23 @@ DataProvider.prototype.settings = function () { }; DataProvider.prototype.settings.browse = function (_args, callback) { - models.Settings.forge(_args).fetch().then(function(settings){ + models.Settings.forge(_args).fetch().then(function (settings) { callback(null, settings); }, callback); }; DataProvider.prototype.settings.read = function (_key, callback) { - models.Settings.forge({ key: _key }).fetch().then(function(setting){ + models.Setting.forge({ key: _key }).fetch().then(function (setting) { callback(null, setting); }, callback); }; - DataProvider.prototype.settings.edit = function (_key, _value, callback) { - var promise; - if (typeof _key === 'object') { - promise = when.all(_.map(settings, function (value, key) { - models.Setting.forge({ key: _key }).set('value', _value).save(); - })); - } else { - promise = models.Setting.forge({ key: _key }).set('value', _value).save(); - } - promise.then(function (settings) { + DataProvider.prototype.settings.edit = function (_data, callback) { + when.all(_.map(_data, function (value, key) { + return models.Setting.forge({ key: key }).fetch().then(function (setting) { + return setting.set('value', value).save(); + }); + })).then(function (settings) { callback(null, settings); }, callback); };