MDL-52204 js: fix 'used before defined' errors
authorDan Poltawski <dan@moodle.com>
Fri, 27 Nov 2015 15:13:56 +0000 (15:13 +0000)
committerDan Poltawski <dan@moodle.com>
Fri, 4 Dec 2015 11:43:10 +0000 (11:43 +0000)
Newer versions of jshint detect this coding error, we fix it
by simply reordering the definition of methods.

lib/amd/build/form-autocomplete.min.js
lib/amd/build/templates.min.js
lib/amd/src/form-autocomplete.js
lib/amd/src/templates.js

index f8bd0d8..1adc2b0 100644 (file)
Binary files a/lib/amd/build/form-autocomplete.min.js and b/lib/amd/build/form-autocomplete.min.js differ
index 71f74a7..87a7d6c 100644 (file)
Binary files a/lib/amd/build/templates.min.js and b/lib/amd/build/templates.min.js differ
index ec3fb43..24d32c4 100644 (file)
@@ -69,6 +69,52 @@ define(['jquery', 'core/log', 'core/str', 'core/templates', 'core/notification']
         selectionElement.attr('aria-activedescendant', itemId);
     };
 
+    /**
+     * Update the element that shows the currently selected items.
+     *
+     * @method updateSelectionList
+     * @private
+     * @param {Object} options Original options for this autocomplete element.
+     * @param {Object} state State variables for this autocomplete element.
+     * @param {JQuery} originalSelect The JQuery object matching the hidden select list.
+     */
+    var updateSelectionList = function(options, state, originalSelect) {
+        // Build up a valid context to re-render the template.
+        var items = [];
+        var newSelection = $(document.getElementById(state.selectionId));
+        var activeId = newSelection.attr('aria-activedescendant');
+        var activeValue = false;
+
+        if (activeId) {
+            activeValue = $(document.getElementById(activeId)).attr('data-value');
+        }
+        originalSelect.children('option').each(function(index, ele) {
+            if ($(ele).prop('selected')) {
+                items.push( { label: $(ele).html(), value: $(ele).attr('value') } );
+            }
+        });
+        var context = $.extend({ items: items }, options, state);
+
+        // Render the template.
+        templates.render('core/form_autocomplete_selection', context).done(function(newHTML) {
+            // Add it to the page.
+            newSelection.empty().append($(newHTML).html());
+
+            if (activeValue !== false) {
+                // Reselect any previously selected item.
+                newSelection.children('[aria-selected=true]').each(function(index, ele) {
+                    if ($(ele).attr('data-value') === activeValue) {
+                        activateSelection(index, state);
+                    }
+                });
+            }
+        }).fail(notification.exception);
+        // Because this function get's called after changing the selection, this is a good place
+        // to trigger a change notification.
+        originalSelect.change();
+    };
+
+
     /**
      * Remove the given item from the list of selected things.
      *
@@ -360,51 +406,6 @@ define(['jquery', 'core/log', 'core/str', 'core/templates', 'core/notification']
         closeSuggestions(state);
     };
 
-    /**
-     * Update the element that shows the currently selected items.
-     *
-     * @method updateSelectionList
-     * @private
-     * @param {Object} options Original options for this autocomplete element.
-     * @param {Object} state State variables for this autocomplete element.
-     * @param {JQuery} originalSelect The JQuery object matching the hidden select list.
-     */
-    var updateSelectionList = function(options, state, originalSelect) {
-        // Build up a valid context to re-render the template.
-        var items = [];
-        var newSelection = $(document.getElementById(state.selectionId));
-        var activeId = newSelection.attr('aria-activedescendant');
-        var activeValue = false;
-
-        if (activeId) {
-            activeValue = $(document.getElementById(activeId)).attr('data-value');
-        }
-        originalSelect.children('option').each(function(index, ele) {
-            if ($(ele).prop('selected')) {
-                items.push( { label: $(ele).html(), value: $(ele).attr('value') } );
-            }
-        });
-        var context = $.extend({ items: items }, options, state);
-
-        // Render the template.
-        templates.render('core/form_autocomplete_selection', context).done(function(newHTML) {
-            // Add it to the page.
-            newSelection.empty().append($(newHTML).html());
-
-            if (activeValue !== false) {
-                // Reselect any previously selected item.
-                newSelection.children('[aria-selected=true]').each(function(index, ele) {
-                    if ($(ele).attr('data-value') === activeValue) {
-                        activateSelection(index, state);
-                    }
-                });
-            }
-        }).fail(notification.exception);
-        // Because this function get's called after changing the selection, this is a good place
-        // to trigger a change notification.
-        originalSelect.change();
-    };
-
     /**
      * Select the currently active item from the suggestions list.
      *
index 6e4bc0c..bde7e0d 100644 (file)
@@ -52,6 +52,83 @@ define([ 'core/mustache',
     /** @var {String} themeName for the current render */
     var currentThemeName = '';
 
+    /**
+     * Load a template from the cache or local storage or ajax request.
+     *
+     * @method getTemplate
+     * @private
+     * @param {string} templateName - should consist of the component and the name of the template like this:
+     *                              core/menu (lib/templates/menu.mustache) or
+     *                              tool_bananas/yellow (admin/tool/bananas/templates/yellow.mustache)
+     * @return {Promise} JQuery promise object resolved when the template has been fetched.
+     */
+    var getTemplate = function(templateName, async) {
+        var deferred = $.Deferred();
+        var parts = templateName.split('/');
+        var component = parts.shift();
+        var name = parts.shift();
+
+        var searchKey = currentThemeName + '/' + templateName;
+
+        // First try request variables.
+        if (searchKey in templateCache) {
+            deferred.resolve(templateCache[searchKey]);
+            return deferred.promise();
+        }
+
+        // Now try local storage.
+        var cached = storage.get('core_template/' + searchKey);
+
+        if (cached) {
+            deferred.resolve(cached);
+            templateCache[searchKey] = cached;
+            return deferred.promise();
+        }
+
+        // Oh well - load via ajax.
+        var promises = ajax.call([{
+            methodname: 'core_output_load_template',
+            args:{
+                component: component,
+                template: name,
+                themename: currentThemeName
+            }
+        }], async, false);
+
+        promises[0].done(
+            function (templateSource) {
+                storage.set('core_template/' + searchKey, templateSource);
+                templateCache[searchKey] = templateSource;
+                deferred.resolve(templateSource);
+            }
+        ).fail(
+            function (ex) {
+                deferred.reject(ex);
+            }
+        );
+        return deferred.promise();
+    };
+
+    /**
+     * Load a partial from the cache or ajax.
+     *
+     * @method partialHelper
+     * @private
+     * @param {string} name The partial name to load.
+     * @return {string}
+     */
+    var partialHelper = function(name) {
+        var template = '';
+
+        getTemplate(name, false).done(
+            function(source) {
+                template = source;
+            }
+        ).fail(notification.exception);
+
+        return template;
+    };
+
     /**
      * Render image icons.
      *
@@ -92,26 +169,6 @@ define([ 'core/mustache',
         return result.trim();
     };
 
-    /**
-     * Load a partial from the cache or ajax.
-     *
-     * @method partialHelper
-     * @private
-     * @param {string} name The partial name to load.
-     * @return {string}
-     */
-    var partialHelper = function(name) {
-        var template = '';
-
-        getTemplate(name, false).done(
-            function(source) {
-                template = source;
-            }
-        ).fail(notification.exception);
-
-        return template;
-    };
-
     /**
      * Render blocks of javascript and save them in an array.
      *
@@ -270,63 +327,6 @@ define([ 'core/mustache',
         return deferred.promise();
     };
 
-    /**
-     * Load a template from the cache or local storage or ajax request.
-     *
-     * @method getTemplate
-     * @private
-     * @param {string} templateName - should consist of the component and the name of the template like this:
-     *                              core/menu (lib/templates/menu.mustache) or
-     *                              tool_bananas/yellow (admin/tool/bananas/templates/yellow.mustache)
-     * @return {Promise} JQuery promise object resolved when the template has been fetched.
-     */
-    var getTemplate = function(templateName, async) {
-        var deferred = $.Deferred();
-        var parts = templateName.split('/');
-        var component = parts.shift();
-        var name = parts.shift();
-
-        var searchKey = currentThemeName + '/' + templateName;
-
-        // First try request variables.
-        if (searchKey in templateCache) {
-            deferred.resolve(templateCache[searchKey]);
-            return deferred.promise();
-        }
-
-        // Now try local storage.
-        var cached = storage.get('core_template/' + searchKey);
-
-        if (cached) {
-            deferred.resolve(cached);
-            templateCache[searchKey] = cached;
-            return deferred.promise();
-        }
-
-        // Oh well - load via ajax.
-        var promises = ajax.call([{
-            methodname: 'core_output_load_template',
-            args:{
-                component: component,
-                template: name,
-                themename: currentThemeName
-            }
-        }], async, false);
-
-        promises[0].done(
-            function (templateSource) {
-                storage.set('core_template/' + searchKey, templateSource);
-                templateCache[searchKey] = templateSource;
-                deferred.resolve(templateSource);
-            }
-        ).fail(
-            function (ex) {
-                deferred.reject(ex);
-            }
-        );
-        return deferred.promise();
-    };
-
     /**
      * Execute a block of JS returned from a template.
      * Call this AFTER adding the template HTML into the DOM so the nodes can be found.