Working settings

This commit is contained in:
Ricardo Tomasi 2013-05-23 10:57:37 -03:00
parent 28bfa8feeb
commit 1af28bc2ac
5 changed files with 49 additions and 37 deletions

View File

@ -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));

View File

@ -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) {

View File

@ -27,11 +27,12 @@
</section>
</header>
<section class="content">
{{#with settings}}
<form id="settings-general">
<fieldset>
<label>
<b>Blog Title</b>
<input id="blog-title" type="text" value="John O'Nolan" />
<input id="blog-title" type="text" value="{{title}}" />
<p>How your blog name appears on the site</p>
</label>
@ -81,7 +82,7 @@
</fieldset>
{{/with}}
</form>
</section>
</section>

View File

@ -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);
}
};

View File

@ -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);
};