From 287ebe79ecf67faaf104538fc2ef086b8dafe864 Mon Sep 17 00:00:00 2001 From: sacr3dc0w Date: Thu, 13 Sep 2018 22:04:07 -0700 Subject: [PATCH] Blueprint for Mapping Custom Templates to JavaScript Modules --- CHANGELOG.md | 1 + assets/js/app.js | 13 +++++++++++++ templates/layout/base.html | 1 + 3 files changed, 15 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d7c3d3a277..7f9d01d8d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog ## Draft +- Blueprint for Mapping Custom Templates to JavaScript Modules [#1346](https://github.com/bigcommerce/cornerstone/pull/1346) ## 2.4.0 (2018-09-14) - Fix encoding issues on Account Signup Form ("'" characters showing in country name)[#1341] (https://github.com/bigcommerce/cornerstone/pull/1341) diff --git a/assets/js/app.js b/assets/js/app.js index fa39211325..cbb2416bd9 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -50,6 +50,8 @@ const pageClasses = { wishlists: () => import('./theme/wishlist'), }; +const customClasses = {}; + /** * This function gets added to the global window and then called * on page load with the current template loaded and JS Context passed in @@ -59,6 +61,8 @@ const pageClasses = { */ window.stencilBootstrap = function stencilBootstrap(pageType, contextJSON = null, loadGlobal = true) { const context = JSON.parse(contextJSON || '{}'); + const template = context.template; + const templateCheck = Object.keys(customClasses).indexOf(template); return { load() { @@ -74,6 +78,15 @@ window.stencilBootstrap = function stencilBootstrap(pageType, contextJSON = null const PageClass = (await pageClassImporter()).default; PageClass.load(context); } + + if (templateCheck > -1) { + // Find the appropriate page loader based on template + const customClassImporter = customClasses[template]; + if (typeof customClassImporter === 'function') { + const CustomClass = (await customClassImporter()).default; + CustomClass.load(context); + } + } }); }, }; diff --git a/templates/layout/base.html b/templates/layout/base.html index 0ed6e7fa29..935d9d760d 100644 --- a/templates/layout/base.html +++ b/templates/layout/base.html @@ -25,6 +25,7 @@ {{inject 'genericError' (lang 'common.generic_error')}} {{inject 'maintenanceMode' settings.maintenance}} {{inject 'urls' urls}} + {{inject 'template' template}} {{{snippet 'htmlhead'}}}