forked from cliftonc/calipso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlocks.js
84 lines (66 loc) · 1.82 KB
/
Blocks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*!
* Calipso Core Library - Storage of Rendered Blocks
* Copyright(c) 2011 Clifton Cunningham
* MIT Licensed
*
* This class controls the storage and retrieval of blocks rendered via the Router, e.g. specific pieces of output.
*
*/
var rootpath = process.cwd() + '/',
path = require('path');
/**
* Holder for rendered blocks (get / set)
* Idea is that this will give us an opportunity
* to cache expensive sections of a page.
*/
function RenderedBlocks(cache) {
this.content = {};
this.contentCache = {};
this.cache = cache;
}
/**
* Set block content
*/
RenderedBlocks.prototype.set = function (block, content, layout, next) {
var calipso = require(path.join(rootpath, 'lib/calipso'));
var cacheKey = calipso.helpers.getCacheKey('block',block,true);
this.content[block] = this.content[block] || [];
this.content[block].push(content);
// If we are caching, then cache it.
if(this.contentCache[block]) {
this.cache.set(cacheKey,{content:content,layout:layout},null,next);
} else {
next();
}
};
/**
* Get block content
*/
RenderedBlocks.prototype.get = function (key, next) {
// Check to see if the key is a regex
if (typeof key === 'function') {
var item, items = [];
for (item in this.content) {
if (this.content.hasOwnProperty(item)) {
if (item.match(key)) {
items.push(this.content[item]);
}
}
}
next(null, items);
} else {
next(null, this.content[key] || []);
}
};
/**
* Get content from cache and load into block
*/
RenderedBlocks.prototype.getCache = function (key, block, next) {
var self = this;
this.cache.get(key,function(err,cache) {
self.content[block] = self.content[block] || [];
self.content[block].push(cache.content);
next(err,cache.layout);
});
}
module.exports.RenderedBlocks = RenderedBlocks;