Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

templateAdapter: avoid dynamic require if possible #394

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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