Skip to content

Commit

Permalink
Migrate settings page to Backbone
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobeat committed Jun 8, 2013
1 parent c82e597 commit e2b3c40
Show file tree
Hide file tree
Showing 10 changed files with 357 additions and 281 deletions.
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
ghost.app().get('/ghost/editor/:id', auth, admin.editor);
ghost.app().get('/ghost/editor', auth, admin.editor);
ghost.app().get('/ghost/content', auth, admin.content);
ghost.app().get('/ghost/settings', auth, admin.settings);
ghost.app().get('/ghost/settings*', auth, admin.settings);
ghost.app().get('/ghost/debug', auth, admin.debug.index);
ghost.app().get('/ghost/debug/db/delete/', auth, admin.debug.dbdelete);
ghost.app().get('/ghost/debug/db/populate/', auth, admin.debug.dbpopulate);
Expand Down
16 changes: 16 additions & 0 deletions core/admin/assets/js/models/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*global window, document, Ghost, $, Backbone, _ */
(function () {
"use strict";

// Set the url manually and id to '0' to force PUT requests
Ghost.Models.Settings = Backbone.Model.extend({
url: '/api/v0.1/settings/',
id: "0",
defaults: {
title: 'My Blog',
description: '',
email: '[email protected]'
}
});

}());
17 changes: 12 additions & 5 deletions core/admin/assets/js/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@
Ghost.Router = Backbone.Router.extend({

routes: {
'content/': 'blog',
'editor': 'editor',
'editor/': 'editor',
'editor/:id': 'editor'
'content/' : 'blog',
'settings/' : 'settings',
'settings(/:pane)' : 'settings',
'editor/' : 'editor',
'editor(/:id)' : 'editor'
},

blog: function () {
var posts = new Ghost.Collections.Posts();
posts.fetch({data: {status: 'all'}}).then(function () {
posts.fetch({ data: { status: 'all' } }).then(function () {
Ghost.currentView = new Ghost.Views.Blog({ el: '#main', collection: posts });
});
},

settings: function (pane) {
var settings = new Ghost.Models.Settings();
settings.fetch().then(function () {
Ghost.currentView = new Ghost.Views.Settings({ el: '#main', model: settings, pane: pane });
});
},

editor: function (id) {
Expand Down
60 changes: 0 additions & 60 deletions core/admin/assets/js/settings.js

This file was deleted.

135 changes: 135 additions & 0 deletions core/admin/assets/js/views/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*global window, document, Ghost, Backbone, $, _, alert */
(function () {
"use strict";

var Settings = {};

// Base view
// ----------
Ghost.Views.Settings = Ghost.View.extend({
initialize: function (options) {
this.addSubview(new Settings.Sidebar({
el: '.settings-sidebar',
pane: options.pane,
model: this.model
}));

this.$('input').iCheck({
checkboxClass: 'icheckbox_square-grey'
});
}
});

// Sidebar (tabs)
// ---------------
Settings.Sidebar = Ghost.View.extend({
initialize: function (options) {
this.menu = this.$('.settings-menu');
this.showContent(options.pane || 'general');
},

events: {
'click .settings-menu li' : 'switchPane'
},

switchPane: function (e) {
e.preventDefault();
var item = $(e.currentTarget),
id = item.find('a').attr('href').substring(1);
this.showContent(id);
},

showContent: function (id) {
Backbone.history.navigate('/settings/' + id);
if (this.pane && '#' + id === this.pane.el) {
return;
}
_.result(this.pane, 'destroy');
this.setActive(id);
this.pane = new Settings[id]({ model: this.model });
this.pane.render();
},

setActive: function (id) {
this.menu.find('li').removeClass('active');
this.menu.find('a[href=#' + id + ']').parent().addClass('active');
}
});

// Content panes
// --------------
Settings.Pane = Ghost.View.extend({
destroy: function () {
this.$el.removeClass('active');
},

render: function () {
this.$el.addClass('active');
}
});

// TODO: render templates on the client
// TODO: use some kind of data-binding for forms

// ### General settings
Settings.general = Settings.Pane.extend({
el: '#general',
events: {
'click .button-save': 'saveSettings'
},

saveSettings: function () {
this.model.save({
title: this.$('#blog-title').val(),
email: this.$('#email-address').val()
}, {
success: function () {
alert('Saved');
}
});
},

render: function () {
var settings = this.model.toJSON();
this.$('#blog-title').val(settings.title);
this.$('#email-address').val(settings.email);
Settings.Pane.prototype.render.call(this);
}
});

// ### Content settings
Settings.content = Settings.Pane.extend({
el: '#content',
events: {
}
});

// ### User settings
Settings.users = Settings.Pane.extend({
el: '#users',
events: {
}
});

// ### Appearance settings
Settings.appearance = Settings.Pane.extend({
el: '#appearance',
events: {
}
});

// ### Services settings
Settings.services = Settings.Pane.extend({
el: '#services',
events: {
}
});

// ### Plugins settings
Settings.plugins = Settings.Pane.extend({
el: '#plugins',
events: {
}
});

}());
2 changes: 0 additions & 2 deletions core/admin/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@
'settings': function (req, res) {
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'),
Expand Down
2 changes: 2 additions & 0 deletions core/admin/views/default.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@

<!-- // require '/core/admin/assets/js/models/*' -->
<script src="/core/admin/assets/js/models/post.js"></script>
<script src="/core/admin/assets/js/models/settings.js"></script>
<!-- // require '/core/admin/assets/js/views/*' -->
<script src="/core/admin/assets/js/views/base.js"></script>
<script src="/core/admin/assets/js/views/blog.js"></script>
<script src="/core/admin/assets/js/views/editor.js"></script>
<script src="/core/admin/assets/js/views/settings.js"></script>
<script src="/core/admin/assets/js/router.js"></script>
{{{block "bodyScripts"}}}
<script>
Expand Down
Loading

0 comments on commit e2b3c40

Please sign in to comment.