Use requirejs.
authorJason Skomorowski <jason@indexdata.com>
Tue, 12 Aug 2014 18:54:48 +0000 (14:54 -0400)
committerJason Skomorowski <jason@indexdata.com>
Tue, 12 Aug 2014 20:20:44 +0000 (16:20 -0400)
mkwsxb/mkwsxb.py
mkwsxb/static/js/src/mkwsxb.js

index fe061b9..13822ea 100644 (file)
@@ -30,10 +30,11 @@ class MKWSXB(XBlock):
         """The primary view of the MKWS XBlock, shown to students when viewing courses."""
         html = self.resource_string("static/html/mkwsxb.html")
         frag = Fragment(html.format(query=self.query, team=random.randint(0, 100000)))
-        frag.add_javascript_url("//mkws.indexdata.com/mkws-complete.js")
-        frag.add_javascript_url("//example.indexdata.com/mkws-widget-ru.js")
-        frag.add_css(self.resource_string("static/css/mkws-widget-ru.css"))
+        # mkwsxb.js uses require.js as it cannot guarantee mkws-complete.js has loaded 
+        # in studio without it
+        frag.add_javascript_url("/static/js/vendor/require.js");
         frag.add_javascript(self.resource_string("static/js/src/mkwsxb.js"))
+        frag.add_css(self.resource_string("static/css/mkws-widget-ru.css"))
         frag.initialize_js('MKWSXB')
         return frag;
 
index 88b9042..dcf79e6 100644 (file)
@@ -1,6 +1,22 @@
 /* Javascript for MKWSXB. */
+require.config({
+  paths: {
+    mkws: "//mkws.indexdata.com/mkws-complete",
+    mkws_widget_ru: "//example.indexdata.com/mkws-widget-ru"
+  },
+  shim: {
+    mkws: {
+      exports: "mkws"
+    },
+    mkws_widget_ru: {
+      deps: ["mkws"]
+    }
+  }
+});
 function MKWSXB(runtime, element) {
-  $(function ($) {
-    mkws.init();
-  });
+  require(['mkws_widget_ru'], function() { 
+    console.log(mkws);
+    //mkws.init("XBlock initialised.", element);
+    mkws.init("XBlock initialised.");
+  } );
 }