Skip to content

Instantly share code, notes, and snippets.

@joshrhoades
Created June 21, 2013 19:53
Show Gist options
  • Save joshrhoades/5833858 to your computer and use it in GitHub Desktop.
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…
/*
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