From adc7f606891c381d4b3c775012113adfdeb9262e Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Sat, 30 Aug 2014 22:47:00 +0100 Subject: [PATCH] templateAdapter: avoid dynamic require if possible --- shared/app.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/shared/app.js b/shared/app.js index 58ca275d..921aff95 100644 --- a/shared/app.js +++ b/shared/app.js @@ -17,8 +17,7 @@ if (!isServer) { module.exports = Backbone.Model.extend({ defaults: { - loading: false, - templateAdapter: 'rendr-handlebars' + loading: false }, /** @@ -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. @@ -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 */