Skip to content

Commit

Permalink
removed global rendr.entryPath
Browse files Browse the repository at this point in the history
in favor of passing entryPath as a server option.

modelUtils is now an object on App instead of a requireable util.

The viewEngine caches layouts based on entryPath.
  • Loading branch information
trshafer committed Nov 22, 2013
1 parent 4803e20 commit a5c6432
Show file tree
Hide file tree
Showing 21 changed files with 263 additions and 223 deletions.
12 changes: 6 additions & 6 deletions client/router.js
Expand Up @@ -7,12 +7,6 @@ Backbone = require('backbone');
BaseRouter = require('../shared/base/router');
BaseView = require('../shared/base/view');

try {
AppView = require(rendr.entryPath + 'app/views/app_view');
} catch (e) {
AppView = require('./app_view');
}

extractParamNamesRe = /:(\w+)/g;

plusRe = /\+/g;
Expand Down Expand Up @@ -58,6 +52,12 @@ ClientRouter.prototype.reverseRoutes = true;
ClientRouter.prototype.initialize = function(options) {
this.app = options.app;

try {
AppView = require(options.entryPath + 'app/views/app_view');
} catch (e) {
AppView = require('./app_view');
}

// We do this here so that it's available in AppView initialization.
this.app.router = this;

Expand Down
8 changes: 2 additions & 6 deletions index.js
@@ -1,13 +1,9 @@
if (!this.window) {
var Server = require('./server/server')

exports.Server = Server;

exports.server = null;
exports.Server = Server

exports.createServer = function(options) {
return exports.server = new Server(options);
return new Server(options);
};

exports.entryPath = process.cwd() + '/';
}
5 changes: 3 additions & 2 deletions server/middleware/initApp.js
Expand Up @@ -13,7 +13,7 @@ var _ = require('underscore');
module.exports = function(appAttributes, options) {
options = options || {};
return function(req, res, next) {
var App = require(rendr.entryPath + 'app/app');
var App = require(options.entryPath + 'app/app');

/**
* Pass any data that needs to be accessible by the client
Expand All @@ -25,7 +25,8 @@ module.exports = function(appAttributes, options) {
* Hold on to a copy of the original request, so we can pull headers, etc.
* This will only be accessible on the server.
*/
req: req
req: req,
entryPath: options.entryPath
};

/**
Expand Down
40 changes: 25 additions & 15 deletions server/server.js
Expand Up @@ -9,23 +9,32 @@ var _ = require('underscore')

module.exports = Server;

var defaultOptions = {
dataAdapter: null,
dataAdapterConfig: null,
viewEngine: null,
errorHandler: null,
notFoundHandler: null,
apiPath: '/api',
appData: {},
paths: {},
viewsPath: null,
defaultEngine: 'js'
};
function defaultOptions(){
return {
dataAdapter: null,
dataAdapterConfig: null,
viewEngine: null,
errorHandler: null,
notFoundHandler: null,
mountPath: null,
apiPath: '/api',
appData: {},
paths: {},
viewsPath: null,
defaultEngine: 'js',
entryPath: process.cwd() + '/'
};
}


function Server(options) {
if(typeof rendr !== 'undefined' && rendr.entryPath){
console.warn("Setting rendr.entryPath is now deprecated. Please pass in entryPath when initializing the rendr server.")
options.entryPath = rendr.entryPath;
}

this.options = options || {};
_.defaults(this.options, defaultOptions);
_.defaults(this.options, defaultOptions());

this.expressApp = express();

Expand All @@ -42,7 +51,7 @@ function Server(options) {
* Tell Express to use our ViewEngine to handle .js, .coffee files.
* This can always be overridden in your app.
*/
this.expressApp.set('views', this.options.viewsPath || (rendr.entryPath + 'app/views'));
this.expressApp.set('views', this.options.viewsPath || (this.options.entryPath + 'app/views'));
this.expressApp.set('view engine', this.options.defaultEngine);
this.expressApp.engine(this.options.defaultEngine, this.viewEngine.render);

Expand Down Expand Up @@ -97,7 +106,8 @@ Server.prototype.configure = function(fn) {
* Initialize the Rendr app, accessible at `req.rendrApp`.
*/
this.expressApp.use(middleware.initApp(this.options.appData, {
apiPath: this.options.apiPath
apiPath: this.options.apiPath,
entryPath: this.options.entryPath
}));

/**
Expand Down
19 changes: 9 additions & 10 deletions server/viewEngine.js
Expand Up @@ -2,7 +2,7 @@

var path = require('path')
, _ = require('underscore')
, layoutTemplate;
, layoutTemplates = {};

module.exports = exports = ViewEngine;

Expand Down Expand Up @@ -46,13 +46,13 @@ ViewEngine.prototype.renderWithLayout = function renderWithLayout(locals, app, c
ViewEngine.prototype.getLayoutTemplate = function getLayoutTemplate(app, callback) {
var layoutPath;

if (layoutTemplate) {
return callback(null, layoutTemplate);
if (layoutTemplates[app.options.entryPath]) {
return callback(null, layoutTemplates[app.options.entryPath]);
}
app.templateAdapter.getLayout('__layout', function(err, template) {
app.templateAdapter.getLayout('__layout', app.options.entryPath, function(err, template) {
if (err) return callback(err);
layoutTemplate = template;
callback(err, layoutTemplate);
layoutTemplates[app.options.entryPath] = template;
callback(err, template);
});
};

Expand All @@ -66,17 +66,16 @@ ViewEngine.prototype.getViewHtml = function getViewHtml(viewPath, locals, app) {
// Pass in the app.
locals.app = app;
name = viewPath.substr(viewPath.indexOf(basePath) + basePath.length + 1);
View = BaseView.getView(name);
View = BaseView.getView(name, app.options.entryPath);
view = new View(locals);
return view.getHtml();
};

ViewEngine.prototype.getBootstrappedData = function getBootstrappedData(locals, app) {
var modelUtils = require('../shared/modelUtils')
, bootstrappedData = {};
var bootstrappedData = {};

_.each(locals, function(modelOrCollection, name) {
if (modelUtils.isModel(modelOrCollection) || modelUtils.isCollection(modelOrCollection)) {
if (app.modelUtils.isModel(modelOrCollection) || app.modelUtils.isCollection(modelOrCollection)) {
bootstrappedData[name] = {
summary: app.fetcher.summarize(modelOrCollection),
data: modelOrCollection.toJSON()
Expand Down
13 changes: 10 additions & 3 deletions shared/app.js
Expand Up @@ -5,11 +5,14 @@
* The client also subclasses it for client-specific stuff.
*/

var Backbone, ClientRouter, Fetcher;
var Backbone, ClientRouter, Fetcher, clientEntryPath, ModelUtils;

require('./globals');
Backbone = require('backbone');
Fetcher = require('./fetcher');
ModelUtils = require('./modelUtils')

clientEntryPath = '';

if (!global.isServer) {
// client side only, entryPath is always empty
Expand All @@ -31,6 +34,9 @@ module.exports = Backbone.Model.extend({
initialize: function(attributes, options) {
this.options = options || {};

entryPath = this.options.entryPath || clientEntryPath
this.modelUtils = this.options.modelUtils || new ModelUtils(entryPath);

/**
* On the server-side, you can access the Express request, `req`.
*/
Expand All @@ -42,7 +48,7 @@ module.exports = Backbone.Model.extend({
* Initialize the `templateAdapter`, allowing application developers to use whichever
* templating system they want.
*/
this.templateAdapter = require(this.get('templateAdapter'));
this.templateAdapter = require(this.get('templateAdapter'))({entryPath: entryPath});

/**
* Instantiate the `Fetcher`, which is used on client and server.
Expand All @@ -56,7 +62,8 @@ module.exports = Backbone.Model.extend({
*/
if (!global.isServer) {
new ClientRouter({
app: this
app: this,
entryPath: clientEntryPath
});
}

Expand Down
2 changes: 1 addition & 1 deletion shared/base/router.js
Expand Up @@ -43,7 +43,7 @@ BaseRouter.prototype._initOptions = function(options) {

this.options = options || {};
paths = this.options.paths = this.options.paths || {};
paths.entryPath = paths.entryPath || rendr.entryPath;
paths.entryPath = paths.entryPath || options.entryPath;
paths.routes = paths.routes || paths.entryPath + 'app/routes';
paths.controllerDir = paths.controllerDir || paths.entryPath + 'app/controllers';
};
Expand Down
22 changes: 13 additions & 9 deletions shared/base/view.js
@@ -1,19 +1,19 @@
/*global rendr*/

var Backbone, BaseView, modelUtils, _;
var Backbone, BaseView, _;

_ = require('underscore');
Backbone = require('backbone');
modelUtils = require('../modelUtils');

function noop() {}

module.exports = BaseView = Backbone.View.extend({
initialize: function(options) {
var obj;

this.name = this.name || modelUtils.underscorize(this.constructor.id || this.constructor.name);
this.parseOptions(options);

this.name = this.name || this.app.modelUtils.underscorize(this.constructor.id || this.constructor.name);
this.postInitialize();
if ((obj = this.model || this.collection) && this.renderOnRefresh) {
obj.on('refresh', this.render, this);
Expand All @@ -38,6 +38,8 @@ module.exports = BaseView = Backbone.View.extend({

if (options.app != null) {
this.app = this.options.app;
}else {
throw new Error("options.app expected when initializing a new view")
}

if (options.parentView != null) {
Expand All @@ -46,16 +48,16 @@ module.exports = BaseView = Backbone.View.extend({

if (options.model != null) {
if (!(options.model instanceof Backbone.Model) && options.model_name) {
options.model = modelUtils.getModel(options.model_name, options.model, {
options.model = this.app.modelUtils.getModel(options.model_name, options.model, {
parse: true
});
}
options.model_name = options.model_name || modelUtils.modelName(options.model.constructor);
options.model_name = options.model_name || this.app.modelUtils.modelName(options.model.constructor);
options.model_id = options.model.id;
}

if (options.collection != null) {
options.collection_name = options.collection_name || modelUtils.modelName(options.collection.constructor);
options.collection_name = options.collection_name || this.app.modelUtils.modelName(options.collection.constructor);
options.collection_params = options.collection.params;
}

Expand Down Expand Up @@ -149,7 +151,7 @@ module.exports = BaseView = Backbone.View.extend({
* Get HTML attributes to add to el.
*/
getAttributes: function() {
var attributes = {}, fetchSummary = {};
var attributes = {}, fetchSummary = {}, modelUtils = this.app.modelUtils;

if (this.attributes) {
_.extend(attributes, _.result(this, 'attributes'));
Expand Down Expand Up @@ -432,8 +434,10 @@ module.exports = BaseView = Backbone.View.extend({
* -------------
*/

BaseView.getView = function(viewName) {
return require(rendr.entryPath + "app/views/" + viewName);
BaseView.getView = function(viewName, entryPath) {
if (entryPath === undefined)
entryPath = ''
return require(entryPath + "app/views/" + viewName);
};

BaseView.attach = function(app, parentView) {
Expand Down

0 comments on commit a5c6432

Please sign in to comment.