Created
June 21, 2013 19:53
-
-
Save joshrhoades/5833858 to your computer and use it in GitHub Desktop.
Make a dynamically injected function always execute, without the delay of callbacks or the evil of `eval`. Amazingly simple solution that works like a charm. I needed this because I wanted to make dynamically precompiled templates instantly available (in PROD mode they are compiled into a JS file, and injected if a module on the page requires it…
This file contains hidden or 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
/* | |
using jQuery AJAX as an example, though we do not leverage jQuery in our Production app. | |
This technique can be used anywhere with injected data/functionality | |
*/ | |
$.ajax({ | |
url: theTemplate.filePath, | |
success: function(data) { | |
/* | |
Take the raw data of the file, insert it into a new function, and execute it. | |
Bypasses need for callbacks, instantly executes it without delay, and does it without using `eval`. | |
Awwwww yeah, suck it eval and callback, you mouthbreathing losers. | |
*/ | |
var thePayload = new Function(data); | |
thePayload(); | |
}, | |
async: false | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment