Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

You can now create sections and pages through the CMS.

  • Loading branch information...
commit 03db44c61c04919df4afff96aefb391f8a051195 1 parent f642786
@fakedarren authored
View
1  app.js
@@ -13,6 +13,7 @@ app.set('view engine', 'jade');
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler());
app.use(express.cookieParser());
+app.use(express.bodyParser());
app.use(express.session({
secret: "My simple node.js CMS"
}));
View
36 backend/controllers/backend/content.js
@@ -7,14 +7,18 @@ var mongoose = require("mongoose"),
api;
-mongoose.model('sections', sectionSchema);
-mongoose.model('pages', pageSchema);
-mongoose.model('grids', gridSchema);
+var Section = mongoose.model('sections', sectionSchema);
+var Page = mongoose.model('pages', pageSchema);
+var Grid = mongoose.model('grids', gridSchema);
api = {
routes: {
+ createSection: {
+ url: '/cms/sections',
+ method: 'post'
+ },
readAllSections: {
url: '/cms/sections',
method: 'get'
@@ -49,6 +53,15 @@ api = {
}
},
+ createSection: function(req, res){
+ Section.create({
+ title: req.body.title
+ }, function(err){
+ res.statusCode = 200;
+ res.send("OK\n");
+ });
+ },
+
readAllSections: function(req, res){
var sections = mongoose.model('sections'),
pages = mongoose.model('pages'),
@@ -94,8 +107,13 @@ api = {
},
createPage: function(req, res){
- res.statusCode = 200;
- res.send("OK\n");
+ Page.create({
+ sectionID: req.body.sectionID,
+ title: req.body.title
+ }, function(err){
+ res.statusCode = 200;
+ res.send("OK\n");
+ });
},
readPage: function(req, res){
@@ -150,6 +168,10 @@ module.exports = _.merge(api, {
url: '/cms/content/new-page',
method: 'get'
},
+ newSection: {
+ url: '/cms/content/new-section',
+ method: 'get'
+ },
grids: {
url: '/cms/content/grids',
method: 'get'
@@ -172,6 +194,10 @@ module.exports = _.merge(api, {
res.render('backend/content/new-page');
},
+ newSection: function(req, res){
+ res.render('backend/content/new-section');
+ },
+
grids: function(req, res){
res.render('backend/content/grids');
},
View
2  public/backend/js/app/models/Page.js
@@ -5,8 +5,10 @@ define([
){
return Backbone.Model.extend({
+ url: '/cms/pages',
defaults: {
id: null,
+ sectionID: '',
title: ''
}
});
View
1  public/backend/js/app/models/Section.js
@@ -5,6 +5,7 @@ define([
){
return Backbone.Model.extend({
+ url: '/cms/sections',
defaults: {
id: null,
title: '',
View
3  public/backend/js/app/pages.js
@@ -9,7 +9,8 @@ require([
){
new SectionListView({
- el: '#pages-content'
+ el: 'body',
+ content: '#pages-content'
})
});
View
2  public/backend/js/app/views/PageList.js
@@ -13,7 +13,7 @@ define([
events: {
'click #create-page button': 'create',
'click .edit': 'edit',
- 'click .remove': 'remove',
+ 'click .remove': 'remove'
},
initialize: function(){
View
68 public/backend/js/app/views/SectionList.js
@@ -1,19 +1,30 @@
define([
'Backbone',
'Handlebars',
+ 'models/Page',
+ 'models/Section',
'models/SectionList'
], function(
Backbone,
Handlebars,
+ Page,
+ Section,
SectionList
){
return Backbone.View.extend({
events: {
+ 'submit #create-section': 'createSection',
+ 'click #add-new-section': 'addSection',
+
+ 'submit #create-page': 'createPage',
+ 'click #add-new-page': 'addPage'
},
initialize: function(){
+ this.$content = this.$el.find(this.options.content);
+
this.collection = new SectionList();
this.collection.fetch();
this.collection.on('sync', this.render, this);
@@ -24,12 +35,61 @@ define([
template = Handlebars.compile(source),
html = template(this.collection.toJSON());
- this.$el.html(html);
- this.$el.find('.nav-tabs a').eq(0).click();
+ this.$content.html(html);
+ this.$content.find('.nav-tabs a').eq(0).click();
+ },
+
+ addSection: function(evt){
+ evt.preventDefault();
+
+ $('#modal').load(evt.target.href, function(){
+ $("#modal").modal("show");
+ });
+ },
+
+ createSection: function(evt){
+ evt.preventDefault();
+
+ var section = new Section({
+ title: $('#create-section [type=text]').val()
+ });
+ section.save();
+
+ this.collection.add(section);
+ this.render();
+
+ $('#modal').modal('hide')
},
- create: function(evt){
- console.log('create');
+ addPage: function(evt){
+ var options = '';
+
+ evt.preventDefault();
+
+ this.collection.forEach(function(section){
+ var details = section.toJSON();
+ options += '<option value="' + details._id + '">' + details.title + '</option>';
+ });
+
+ $('#modal').load(evt.target.href, function(){
+ $("#modal").modal("show");
+ $('#create-page select').html(options);
+ });
+ },
+
+ createPage: function(evt){
+ evt.preventDefault();
+
+ var page = new Page({
+ sectionID: $('#create-page select').val(),
+ title: $('#create-page [type=text]').val()
+ });
+ page.save();
+
+ this.collection.fetch();
+ this.render();
+
+ $('#modal').modal('hide')
},
remove: function(evt){
View
4 views/backend/content/new-page.jade
@@ -8,12 +8,10 @@ div.modal-body
label.control-label Section
div.controls
select
- option Geoff
- option Jeff
div.control-group
label.control-label Page Name
div.controls
input(type="text")
div.control-group
div.controls
- button.btn.btn-primary(type="button") Create Page
+ button.btn.btn-primary(type="submit") Create Page
View
13 views/backend/content/new-section.jade
@@ -0,0 +1,13 @@
+div.modal-header
+ button.close(type="button", data-dismiss="modal", aria-hidden="true") &times;
+ h3 Create a new Section
+div.modal-body
+ form.form-horizontal#create-section(action="#")
+ fieldset
+ div.control-group
+ label.control-label Section Name
+ div.controls
+ input(type="text")
+ div.control-group
+ div.controls
+ button.btn.btn-primary(type="submit") Create Section
View
4 views/backend/content/pages.jade
@@ -15,9 +15,9 @@ block content
div.well
ul
li
- a(href="/cms/content/new-page", data-toggle="modal", data-target="#modal") Add a new Page
+ a#add-new-section(href="/cms/content/new-section") Add a new Section
li
- a(href="#") Add a new section
+ a#add-new-page(href="/cms/content/new-page") Add a new Page
div#modal.modal.hide
div.modal-body
Please sign in to comment.
Something went wrong with that request. Please try again.