Commit 1c18ad96 by Victor Shnayder

make loader only load scripts once per run, midway through fixing multiple chem equations per page

parent b2afa82c
...@@ -41,12 +41,17 @@ class @JavascriptLoader ...@@ -41,12 +41,17 @@ 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.
src = $(placeholder).attr("data-src")
if src not of loaded
loaded[src] = true
s = document.createElement('script') s = document.createElement('script')
s.setAttribute('src', $(placeholder).attr("data-src")) s.setAttribute('src', src)
s.setAttribute('type', "text/javascript") s.setAttribute('type', "text/javascript")
s.onload = completionHandlerGenerator(index) s.onload = completionHandlerGenerator(index)
...@@ -58,3 +63,4 @@ class @JavascriptLoader ...@@ -58,3 +63,4 @@ class @JavascriptLoader
# properly. # properly.
$('head')[0].appendChild(s) $('head')[0].appendChild(s)
$(placeholder).remove() $(placeholder).remove()
(function () { (function () {
var preview_div = $('.chemicalequationinput .equation'); update = function(index, input) {
$('.chemicalequationinput input').bind("input", function(eventObject) { preview_div = $(input).siblings('div.equation');
$.get("/preview/chemcalc/", {"formula" : this.value}, function(response) {
$.get("/preview/chemcalc/", {"formula" : input.value}, function(response) {
if (response.error) { if (response.error) {
preview_div.html("<span class='error'>" + response.error + "</span>"); preview_div.html("<span class='error'>" + response.error + "</span>");
} else { } else {
preview_div.html(response.preview); preview_div.html(response.preview);
} }
}); });
}
inputs = $('.chemicalequationinput input');
// update on load
inputs.each(update);
// and on every change
inputs.bind("input", function(event) {
// pass a dummy index
update(0, event.target);
}); });
}).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