Commit 1ec0fae8 by arjun810

Merge pull request #891 from MITx/feature/victor/capa-ajax

Feature/victor/capa ajax
parents ccc5e577 db8639e0
...@@ -41,20 +41,30 @@ class @JavascriptLoader ...@@ -41,20 +41,30 @@ class @JavascriptLoader
callbackCalled = true callbackCalled = true
callback() if callback? callback() if callback?
# Keep a map of what sources we're loaded from, and don't do it twice.
loaded = {}
placeholders.each (index, placeholder) -> placeholders.each (index, placeholder) ->
# TODO: Check if the script already exists in DOM. If so, (1) copy it # TODO: Check if the script already exists in DOM. If so, (1) copy it
# into memory; (2) delete the DOM script element; (3) reappend it. # into memory; (2) delete the DOM script element; (3) reappend it.
# This would prevent memory bloat and save a network request. # This would prevent memory bloat and save a network request.
s = document.createElement('script') src = $(placeholder).attr("data-src")
s.setAttribute('src', $(placeholder).attr("data-src")) if src not of loaded
s.setAttribute('type', "text/javascript") loaded[src] = true
s = document.createElement('script')
s.setAttribute('src', src)
s.setAttribute('type', "text/javascript")
s.onload = completionHandlerGenerator(index)
s.onload = completionHandlerGenerator(index) # s.onload does not fire in IE8; this does.
s.onreadystatechange = completionHandlerGeneratorIE(index)
# s.onload does not fire in IE8; this does. # Need to use the DOM elements directly or the scripts won't execute
s.onreadystatechange = completionHandlerGeneratorIE(index) # properly.
$('head')[0].appendChild(s)
# Need to use the DOM elements directly or the scripts won't execute else
# properly. # just call the completion callback directly, without reloading the file
$('head')[0].appendChild(s) completionHandlerGenerator(index)()
$(placeholder).remove() $(placeholder).remove()
(function () { (function () {
var preview_div = $('.chemicalequationinput .equation'); update = function() {
$('.chemicalequationinput input').bind("input", function(eventObject) { preview_div = $(this).siblings('div.equation');
$.get("/preview/chemcalc/", {"formula" : this.value}, function(response) {
if (response.error) { function create_handler(saved_div) {
preview_div.html("<span class='error'>" + response.error + "</span>"); return (function(response) {
} else { if (response.error) {
preview_div.html(response.preview); saved_div.html("<span class='error'>" + response.error + "</span>");
} } else {
}); saved_div.html(response.preview);
}); }
});
}
$.get("/preview/chemcalc/", {"formula" : this.value}, create_handler(preview_div));
}
inputs = $('.chemicalequationinput input');
// update on load
inputs.each(update);
// and on every change
inputs.bind("input", update);
}).call(this); }).call(this);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment