Created
November 8, 2016 02:26
-
-
Save gbutt/b16b64f1d0173a8503c75b7b14ce8608 to your computer and use it in GitHub Desktop.
Load angular app from localhost, and fallback to a static resource
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
var j$ = jQuery.noConflict(); | |
if ({! debugEnabled }) { | |
loadSourcesLocalhost().fail(loadSourcesSfdc); | |
} else { | |
loadSourcesSfdc(); | |
} | |
function loadSourcesLocalhost() { | |
return j$.ajax({url: '//localhost:3000/vendor.js', dataType: 'script', timeout: 100}) | |
.done(function () { | |
console.log("using localhost"); | |
var src = { | |
styles: "//localhost:3000/styles.css", | |
app: "//localhost:3000/app.js" | |
}; | |
return loadSource(src); | |
}); | |
} | |
function loadSourcesSfdc() { | |
console.log("using salesforce"); | |
var src = { | |
app: "{! URLFOR( $Resource.ngApp, 'js/app.js' ) }" | |
}; | |
return j$.getScript("{! URLFOR( $Resource.ngApp, 'js/vendor.js' ) }") | |
.then(function() { | |
return loadSource(src); | |
}); | |
} | |
function loadSource(src) { | |
if (src.styles) { | |
j$('head').append(createStylesheet(src.styles)); | |
} | |
return j$.getScript(src.app) | |
.then(function(){ | |
angular.bootstrap(document, ['app']); | |
}); | |
} | |
function createStylesheet(src) { | |
var style = document.createElement("link"); | |
style.rel = "stylesheet"; | |
style.href = src; | |
return style; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment