This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
router-mixin.js
200 lines (175 loc) · 5.46 KB
/
router-mixin.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/** Mixin: Barefoot.Router.Server
* This mixin contains any server side specific code for the <Barefoot.Router>.
*
* See also:
* * <Barefoot.Router>
*/
var _ = require('underscore')
, cheerio = require('cheerio')
, Q = require('q')
, CookieAdapter
, DataStore;
/** Function: preInitialize
* This function is called before any intialization or constructor of the
* <Barefoot.Router> is executed. It expects that the passed options object
* contains the following elements:
*
* app - An Express.JS app
* layoutTemplate - The minimal HTML skeleton to render the applictation into.
* Example: "<html><head></head><body></body></html>"
* startExpressApp - A callback function which initiates the actual startup of
* the Express.JS app from above.
*
* Each of these are copied into this router object directly to access them
* later on.
*
* Parameters:
* (Object) options
*/
function preInitialize(options) {
this.app = options.app;
this.layoutTemplate = options.layoutTemplate;
this.startExpressApp = options.startExpressApp;
this.apiAdapter = options.apiAdapter;
this.setupRequestContext = options.setupRequestContext;
}
/** Function: route
* This replacemente for the route function of Backbone.Router ensures that each
* route defined in the router is added to the Express.JS app.
*
* The route function ensures that you have access to all cookies from the
* request via the cookieAdapter property of this router.
*
* Parameters:
* (String) routeUri - URI
*
* Returns:
* The created Express.JS route
*
* See also:
* * <Barefoot.CookieAdapter.Server>
*/
function route(routeUri) {
var self = this
, Barefoot = require('../')();
// Lazy-Load the barefoot objects CookieAdapter and DataStore. Otherwise
// we'd have some cyclic dependency problems.
CookieAdapter = Barefoot.CookieAdapter;
DataStore = Barefoot.DataStore;
return self.app.get('/' + routeUri, function(req, res) {
var callback = self[self.routes[routeUri]]
, cookieAdapter = new CookieAdapter({ cookies: req.cookies })
, dataStore = new DataStore();
// Ensure that request-local stuff is available locally:
self.req = req;
self.res = res;
self.cookieAdapter = cookieAdapter;
self.dataStore = dataStore;
// Ensure the request context is set up locally:
if(!_.isUndefined(self.setupRequestContext)) {
self.setupRequestContext.call(self);
}
// Ensure that the APIAdapter has access to the current request:
if(!_.isUndefined(self.apiAdapter)) {
self.apiAdapter.req = req;
}
// Execute the route:
var routeResult = callback.apply(self, _.values(req.params));
// Ensure cookies are placed inside the response:
res.cookies = self.cookieAdapter.cookies;
return routeResult;
});
}
/** Function: navigate
* Rewrites <Backbone.Router.navigate at http://backbonejs.org/#Router-navigate>
* to replicate its functionality when rendering the application on the server.
*
* Basically the callback for the given routeUri is picked and called.
*
* Parameters:
* (String) routeUri - The URI of the route to navigate to
*
* Returns:
* (Object) the value which the route callbacks returns.
*/
function navigate(routeUri) {
this.res.redirect(routeUri);
}
/** Function: render
* This function initiates the rendering of the passed view. This is done by
* loading the layout template into a cheerio DOM object.
*
* Afterwards the presence of a main view object is checked. If available, that
* main view gets rendered into the DOM. In any case, the passed view gets
* rendered.
*
* As a final step the presence of registered models in the application wide
* <Barefoot.DataStore> is checked. If present, its content gets serialized into
* a script DOM element so the client can proberly deserialize its state
* afterwards. See <Barefoot.Router.Client.start> for the deserialization code.
*
* The resulting HTML code is then sent to the client by using the Express.JS
* response object, buffered from the route callback in the <route> function.
*
* Parameters:
* (Barefoot.View) view - The view which should be rendered
*
* See also:
* * <route>
*/
function render(view) {
var self = this
, $;
function initDOM() {
$ = cheerio.load(self.layoutTemplate);
}
function renderMainView() {
var promise;
if(!_.isUndefined(self.mainView)) {
var clonedMainView = _.clone(self.mainView());
clonedMainView.$ = $;
clonedMainView.$el = clonedMainView.selectDOMElement($);
promise = clonedMainView.render();
}
return promise;
}
function renderView() {
view.$ = $;
view.$el = view.selectDOMElement($);
return view.render();
}
function serializeDataStore() {
if(!_.isUndefined(self.dataStore) &&
_.keys(self.dataStore.registeredModels).length > 0) {
var serializiedDataStore = JSON.stringify(self.dataStore.toJSON())
, javaScriptElement =
'<script>function deserializeDataStore(){' +
'return ' + serializiedDataStore + ';}</script>';
$('body').append(javaScriptElement);
}
}
function writeHTTPResponse() {
self.res.send($.html());
}
function writeHTTPError() {
self.res.send(500);
}
Q.fcall(initDOM)
.then(renderMainView)
.then(renderView)
.then(serializeDataStore)
.done(writeHTTPResponse, writeHTTPError);
}
/** Function: start
* Calls the passed starter function, buffered in <preInitialize>.
*/
function start() {
this.startExpressApp(this.app);
}
module.exports = {
preInitialize: preInitialize
, route: route
, navigate: navigate
, render: render
, start: start
};