Skip to content

Commit

Permalink
templateAdapter: avoid dynamic require if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Aug 30, 2014
1 parent 3914e0c commit adc7f60
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions shared/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ if (!isServer) {
module.exports = Backbone.Model.extend({

defaults: {
loading: false,
templateAdapter: 'rendr-handlebars'
loading: false
},

/**
Expand Down Expand Up @@ -49,9 +48,12 @@ module.exports = Backbone.Model.extend({
*
* We can't use `this.get('templateAdapter')` here because `Backbone.Model`'s
* constructor has not yet been called.
*
* In order to support more packagers, instead of setting `templateAdapter`,
* you should override the `getTemplateAdapterModule` method.
*/
var templateAdapterModule = attributes.templateAdapter || this.defaults.templateAdapter;
this.templateAdapter = require(templateAdapterModule)({entryPath: entryPath});
var templateAdapterModule = this.getTemplateAdapterModule(attributes.templateAdapter)
this.templateAdapter = templateAdapterModule({entryPath: entryPath});

/**
* Instantiate the `Fetcher`, which is used on client and server.
Expand Down Expand Up @@ -94,6 +96,17 @@ module.exports = Backbone.Model.extend({
return require('../client/app_view');
},

/**
* @shared
*/
getTemplateAdapterModule: function(moduleName) {
if (!moduleName || moduleName === 'rendr-handlebars') {
return require('rendr-handlebars');
} else {
return require(moduleName);
}
},

/**
* @client
*/
Expand Down

0 comments on commit adc7f60

Please sign in to comment.