From 1c18ad960758bc471c7c4bbded0926823aa90909 Mon Sep 17 00:00:00 2001 From: Victor Shnayder Date: Fri, 12 Oct 2012 19:18:58 -0400 Subject: [PATCH] make loader only load scripts once per run, midway through fixing multiple chem equations per page --- .../xmodule/js/src/javascript_loader.coffee | 28 +++++++++++-------- .../js/capa/chemical_equation_preview.js | 18 +++++++++--- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/common/lib/xmodule/xmodule/js/src/javascript_loader.coffee b/common/lib/xmodule/xmodule/js/src/javascript_loader.coffee index c3f536787e..55d79993ff 100644 --- a/common/lib/xmodule/xmodule/js/src/javascript_loader.coffee +++ b/common/lib/xmodule/xmodule/js/src/javascript_loader.coffee @@ -41,20 +41,26 @@ class @JavascriptLoader callbackCalled = true callback() if callback? + # Keep a map of what sources we're loaded from, and don't do it twice. + loaded = {} placeholders.each (index, placeholder) -> # 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. # This would prevent memory bloat and save a network request. - s = document.createElement('script') - s.setAttribute('src', $(placeholder).attr("data-src")) - s.setAttribute('type', "text/javascript") + src = $(placeholder).attr("data-src") + if src not of loaded + 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. - s.onreadystatechange = completionHandlerGeneratorIE(index) - - # Need to use the DOM elements directly or the scripts won't execute - # properly. - $('head')[0].appendChild(s) - $(placeholder).remove() + # Need to use the DOM elements directly or the scripts won't execute + # properly. + $('head')[0].appendChild(s) + $(placeholder).remove() + diff --git a/common/static/js/capa/chemical_equation_preview.js b/common/static/js/capa/chemical_equation_preview.js index 9c5c6cd6bc..95fec0e607 100644 --- a/common/static/js/capa/chemical_equation_preview.js +++ b/common/static/js/capa/chemical_equation_preview.js @@ -1,12 +1,22 @@ (function () { - var preview_div = $('.chemicalequationinput .equation'); - $('.chemicalequationinput input').bind("input", function(eventObject) { - $.get("/preview/chemcalc/", {"formula" : this.value}, function(response) { + update = function(index, input) { + preview_div = $(input).siblings('div.equation'); + + $.get("/preview/chemcalc/", {"formula" : input.value}, function(response) { if (response.error) { preview_div.html("" + response.error + ""); } else { 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);