Last active
July 13, 2016 05:53
-
-
Save justinph/8549895 to your computer and use it in GitHub Desktop.
Dynamically loads css and js assets on MPRnews.org
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(["jquery"], function router() { | |
"use strict"; | |
return { | |
basePath: '', | |
init: function() { | |
var self = this; | |
//Javascript loading needs to happen on initial load and on successful pjax completion | |
this.loadJS(); | |
$(document).on("pjax:complete", function() { | |
self.loadJS(); | |
}); | |
//CSS loading only needs to happen on pjax loading start | |
//we use start because we want the css to load first to prevent FOBUC | |
$(document).on("pjax:start", function() { | |
self.loadCSS(); | |
}); | |
}, | |
updatePath: function() { | |
var pathArr = window.location.pathname.split('/'); | |
this.basePath = pathArr[1].toLowerCase(); | |
}, | |
/** | |
* Helper to translate our css path, which must be defined globally as $css_path in the base.tpl | |
* @param {string} route the name of the route we're loading | |
* @return {string} path to the css asset | |
*/ | |
getCssPath: function(route) { | |
return $css_path.replace('ROUTE', route); | |
}, | |
loadJS: function() { | |
this.updatePath(); | |
switch (this.basePath) { | |
case 'story': | |
//console.log('loading story js'); | |
require(["app/story"]); | |
break; | |
case 'weather-test': | |
require(["app/weather"]); | |
break; | |
} | |
}, | |
loadCSS: function() { | |
this.updatePath(); | |
switch (this.basePath) { | |
case 'story': | |
//console.log('loading story css'); | |
require(['css!' + this.getCssPath('story')]); | |
break; | |
case 'weather-test': | |
require(['css!' + this.getCssPath('weather')]); | |
break; | |
} | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment