From 58926d1ce4e97b2eb3b2ff6254efaae2759b35bd Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Thu, 16 May 2013 21:56:26 +0100 Subject: [PATCH] Updating controllers to use the api + some minor changes to the api calls --- core/admin/controllers/index.js | 8 ++++---- core/frontend/controllers/index.js | 30 ++++++------------------------ core/shared/api.js | 6 +++--- 3 files changed, 13 insertions(+), 31 deletions(-) diff --git a/core/admin/controllers/index.js b/core/admin/controllers/index.js index 8db690013d..c8b9decd43 100644 --- a/core/admin/controllers/index.js +++ b/core/admin/controllers/index.js @@ -61,13 +61,13 @@ }, 'editor': function (req, res) { if (req.params.id !== undefined) { - api.posts.read(parseInt(req.params.id, 10)) + api.posts.read({id: parseInt(req.params.id, 10)}) .then(function (post) { res.render('editor', { bodyClass: 'editor', adminNav: setSelected(adminNavbar, 'blog'), - title: post.title, - content: post.content + title: post.get('title'), + content: post.get('content') }); }); } else { @@ -83,7 +83,7 @@ res.render('blog', { bodyClass: 'manage', adminNav: setSelected(adminNavbar, 'blog'), - posts: posts + posts: posts.toJSON() }); }); }, diff --git a/core/frontend/controllers/index.js b/core/frontend/controllers/index.js index cbe4581e42..cf42783778 100644 --- a/core/frontend/controllers/index.js +++ b/core/frontend/controllers/index.js @@ -7,45 +7,27 @@ 'use strict'; var Ghost = require('../../ghost'), - _ = require('underscore'), + api = require('../../shared/api'), ghost = new Ghost(), frontendControllers; frontendControllers = { 'homepage': function (req, res) { - var featureCount = 0, - postCount = 0, - data; - - ghost.dataProvider().posts.findAll(function (error, posts) { - data = _.groupBy(posts, function (post) { - var group = null; - if (post.featured === true && featureCount < ghost.config().homepage.features) { - featureCount += 1; - group = 'features'; - } else if (postCount < ghost.config().homepage.posts) { - postCount += 1; - group = 'posts'; - } - - return group; - }); - - ghost.doFilter('prepostsRender', data.posts, function (posts) { - res.render('index', {features: data.features, posts: posts, ghostGlobals: ghost.globals()}); + api.posts.browse().then(function (posts) { + ghost.doFilter('prePostsRender', posts.toJSON(), function (posts) { + res.render('index', {posts: posts, ghostGlobals: ghost.globals()}); }); }); }, 'single': function (req, res) { - ghost.dataProvider().posts.findOne({'slug': req.params.slug}, function (error, post) { - ghost.doFilter('prePostsRender', post, function (post) { + api.posts.read({'slug': req.params.slug}).then(function (post) { + ghost.doFilter('prePostsRender', post.toJSON(), function (post) { res.render('single', {post: post, ghostGlobals: ghost.globals()}); }); }); } }; - module.exports = frontendControllers; }()); \ No newline at end of file diff --git a/core/shared/api.js b/core/shared/api.js index 5cc766cad5..cfcb888626 100644 --- a/core/shared/api.js +++ b/core/shared/api.js @@ -23,12 +23,12 @@ // takes filter / pagination parameters // returns a list of posts in a json response browse: function (options) { - return when.call(ghost.dataProvider().posts.findAll); + return when.call(ghost.dataProvider().posts.findAll, options); }, // takes an identifier (id or slug?) // returns a single post in a json response - read: function (id) { - return when.call(ghost.dataProvider().posts.findOne, {id: id}); + read: function (args) { + return when.call(ghost.dataProvider().posts.findOne, args); }, // takes a json object with all the properties which should be updated // returns the resulting post in a json response