Created
February 14, 2014 22:44
-
-
Save christabor/9011010 to your computer and use it in GitHub Desktop.
Declarative and modular approach to http://css-tricks.com/multiple-simultaneous-ajax-requests-one-callback-jquery/
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
<a href="#" data-load-css="wtf.css" data-load-fn="wtfFn">Some module link</a> | |
<form action=""> | |
<button data-load-css="wtf2.css" data-load-fn="wtf2Fn">Yeah buddy</button> | |
</form> | |
var moduleFuncs = { | |
wtfFn: function() {}, | |
wtf2Fn: function() {} | |
}; | |
$('[data-load-css]').on('click.load-modules', loadModule); | |
function loadModule(event) { | |
event.preventDefault(); | |
var url = $(this).data('load-css'); | |
var callback = $(this).data('load-fn'); | |
var urlpromise = $.ajax(url); | |
$.when(urlpromise).then(moduleFuncs[callback]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment