Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Commit

Permalink
playing
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Alabor committed May 21, 2013
1 parent 798aa2e commit 68681ef
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
58 changes: 37 additions & 21 deletions lib/server/router-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
*/
var _ = require('underscore')
, cheerio = require('cheerio')
, Q = require('q')
, CookieAdapter
, DataStore;

/** Function: preInitialize
* This function is called before any intialization or constructor of the
* 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:
*
Expand Down Expand Up @@ -38,7 +39,7 @@ function preInitialize(options) {
* 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
* The route function ensures that you have access to all cookies from the
* request via the cookieAdapter property of this router.
*
* Parameters:
Expand Down Expand Up @@ -113,7 +114,7 @@ function navigate(routeUri) {
* 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
* 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
Expand All @@ -128,35 +129,50 @@ function navigate(routeUri) {
* (Barefoot.View) view - The view which should be rendered
*
* Returns:
*
*
*
* See also:
* * <route>
*/
function render(view) {
var $ = cheerio.load(this.layoutTemplate);

if(!_.isUndefined(this.mainView)) {
var clonedMainView = _.clone(this.mainView());
clonedMainView.$ = $;
clonedMainView.$el = clonedMainView.selectDOMElement($);
clonedMainView.render();
function renderMainView($, mainView) {
if(!_.isUndefined(mainView)) {
var clonedMainView = _.clone(mainView());
clonedMainView.$ = $;
clonedMainView.$el = clonedMainView.selectDOMElement($);
clonedMainView.render();
}
}

function renderView($, view) {
view.$ = $;
view.$el = view.selectDOMElement($);

return view.render();
}

function serializeDataStore($, dataStore) {
if(!_.isUndefined(dataStore) &&
_.keys(dataStore.registeredModels).length > 0) {
var serializiedDataStore = JSON.stringify(dataStore.toJSON())
, javaScriptElement =
'<script>function deserializeDataStore(){' +
'return ' + serializiedDataStore + ';}</script>';
$('body').append(javaScriptElement);
}
}

view.$ = $;
view.$el = view.selectDOMElement($);
view.render();

if(!_.isUndefined(this.dataStore) &&
_.keys(this.dataStore.registeredModels).length > 0) {
var serializiedDataStore = JSON.stringify(this.dataStore.toJSON())
, javaScriptElement =
'<script>function deserializeDataStore(){' +
'return ' + serializiedDataStore + ';}</script>';
$('body').append(javaScriptElement);
function writeResponse() {
this.res.send($.html());
}

return this.res.send($.html());
Q.fcall(loadTemplate)
.then(renderMainView)
.then(renderView)
.then(serializeDataStore)
.then(writeResponse);
}

/** Function: start
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
, "cookie-component": "~0.1.5"
, "methods": "~0.0.1"
, "winston": "~0.7.1"
, "q": "~0.9.3"
}
, "devDependencies": {
"mocha": "~1.8.2"
Expand Down

0 comments on commit 68681ef

Please sign in to comment.