Skip to content
Browse files

Moves the building to better invocation

Also adds postsByLatest to the posts model
  • Loading branch information...
1 parent 350c0ae commit aa09828378226e7d08435cf6f36e86b0be9f2727 @craveytrain committed Oct 22, 2013
Showing with 39 additions and 33 deletions.
  1. +2 −2 models/index.js
  2. +8 −20 models/list.js
  3. +3 −3 models/page.js
  4. +25 −7 models/post.js
  5. +1 −1 tasks/render.js
View
4 models/index.js
@@ -20,12 +20,12 @@ require('fs').readdirSync(__dirname).forEach(function(file) {
modelBuilders[base] = require('./' + base);
});
-module.exports = function(siteModel) {
+module.exports = function() {
var models = {};
// loop through the model builders and attach them to the models object
Object.keys(modelBuilders).forEach(function(pageType) {
- models[pageType] = modelBuilders[pageType](siteModel);
+ models[pageType] = modelBuilders[pageType];
});
return models;
View
28 models/list.js
@@ -2,28 +2,16 @@
var extend = require('extend');
-function postsByLatest (posts) {
- var arr = [];
+var pageModel = require('./page');
+var postModel = require('./post');
- Object.keys(posts).forEach(function(slug) {
- arr.push(posts[slug]['/']);
- });
-
- return arr.sort(function(a, b) {
- return a.date - b.date;
- });
-}
-
-module.exports = function(siteModel) {
- // initialize the page model with the base model content
- var pageModel = require('./page')(siteModel);
-
- return function(data, taxonomy) {
- // pass the view specific data into the page model
- var baseModel = pageModel(data);
+module.exports = {
+ buildModel: function (siteModel, data, taxonomy) {
+ // pass the site model and the view specific data into the page model
+ var baseModel = pageModel.buildModel(siteModel, data);
return extend({}, baseModel, data, {
- posts: postsByLatest(taxonomy.posts)
+ posts: postModel.postsByLatest(taxonomy.posts)
});
- };
+ }
};
View
6 models/page.js
@@ -7,8 +7,8 @@ marked.aliases = {
html: 'xml'
};
-module.exports = function(baseModel) {
- return function(data, taxonomy) {
+module.exports = {
+ buildModel: function (baseModel, data, taxonomy) {
// trim the whitespace from the content, drop it back in the model
var content = data.__content.trim();
@@ -18,5 +18,5 @@ module.exports = function(baseModel) {
content: content,
body: body
});
- };
+ }
};
View
32 models/post.js
@@ -4,18 +4,36 @@ var marked = require('supermarked');
var excerpt = require('../lib/excerpt');
var extend = require('extend');
+var pageModel = require('./page');
-module.exports = function(siteModel) {
- // initialize the page model with the base model content
- var pageModel = require('./page')(siteModel);
+var getPosts = (function(posts) {
+ return function (taxonomyPosts) {
+ // if there are no posts, populate the posts array
+ if (!posts.length) {
+ Object.keys(taxonomyPosts).forEach(function(slug) {
+ posts.push(taxonomyPosts[slug]['/']);
+ });
+ }
- return function(data, taxonomy) {
- // pass the view specific data into the page model
- var baseModel = pageModel(data);
+ return posts;
+ };
+}([]));
+
+module.exports = {
+ buildModel: function (siteModel, data, taxonomy) {
+ // pass the site model and the view specific data into the page model
+ var baseModel = pageModel.buildModel(siteModel, data);
return extend({}, baseModel, data, {
// create excerpt if non exists and mark it down
excerpt: marked(baseModel.excerpt || excerpt(baseModel.content))
});
- };
+ },
+ postsByLatest: function (taxonomyPosts) {
+ var posts = getPosts(taxonomyPosts);
+
+ return posts.sort(function(a, b) {
+ return a.date - b.date;
+ });
+ }
};
View
2 tasks/render.js
@@ -28,7 +28,7 @@ function buildModel (data) {
var modelFile = models[data.pageType] || models.page;
// build up the model and return it
- return modelFile(data, taxonomy);
+ return modelFile.buildModel(siteModel, data, taxonomy);
}
function buildPageTaxonomy (file) {

0 comments on commit aa09828

Please sign in to comment.
Something went wrong with that request. Please try again.