Browse Source

Merge pull request #771 from herby/alternate-find-amberjs-script

amber.js tries to find itself also by id not just as last <script>
Nicolas Petton 11 years ago
parent
commit
730558eec6
2 changed files with 15 additions and 2 deletions
  1. 2 0
      CHANGELOG
  2. 13 2
      support/amber.js

+ 2 - 0
CHANGELOG

@@ -6,6 +6,8 @@ Highlights:
 * JQuery updated to ~1.10.2; jquery-ui updated to match
 * You can create subclasses of `nil`
 * Amber loads in IE8
+* You can load amber.js asynchronously (it must
+  have id 'amber-path-mapper' in that case)
 
 Commits: https://github.com/amber-smalltalk/amber/compare/0.12.2...0.12.2
 Issues:  https://github.com/amber-smalltalk/amber/issues?milestone=__&state=closed

+ 13 - 2
support/amber.js

@@ -15,8 +15,19 @@
 var require;
 
 require = function (require) {
-    var scripts = document.getElementsByTagName("script");
-    var me = scripts[scripts.length - 1];
+    // To be able to use its path and attrubutes
+    // to map other parts of Amber, this code must find its <script> tag.
+    // It first looks for id 'amber-path-mapper'.
+    // When loading amber.js asynchronously, you must include this id,
+    // or the code can not reliably find its <script>.
+    var me = document.getElementById("amber-path-mapper");
+    if (!me || me.tagName.toLowerCase() !== "script") {
+        // If <script> with 'amber-path-mapper' id is not present,
+        // (this id is not necessary for inline <script> tag in HTML),
+        // it uses the "find the last <script> tag present in the moment" method.
+        var scripts = document.getElementsByTagName("script");
+        me = scripts[scripts.length - 1];
+    }
     var src = me.src;
     // strip the last two elements from the URL
     // e.g. http://app.com/amber/support/amber.js -> http://app.com/amber