From 4e630236a130cd495189d88983b54091b1206007 Mon Sep 17 00:00:00 2001 From: Damian Frizzi Date: Fri, 13 Jan 2017 14:12:46 +0100 Subject: [PATCH] Load additional scripts with require if we're in an AMD environement. This fixes https://github.com/victorjonsson/jQuery-Form-Validator/issues/546 and prevents the "mismatched anonymous define() module" error in requirejs when requiring the plugin outside of the normal requirejs environement. --- form-validator/jquery.form-validator.js | 38 ++++++++++++++----------- src/main/module-loader.js | 38 ++++++++++++++----------- 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/form-validator/jquery.form-validator.js b/form-validator/jquery.form-validator.js index 2ab152e..e3922db 100644 --- a/form-validator/jquery.form-validator.js +++ b/form-validator/jquery.form-validator.js @@ -1014,23 +1014,27 @@ $.formUtils.loadedModules[scriptUrl] = 1; hasLoadedAnyModule = true; - // Load the script - script.type = 'text/javascript'; - script.onload = moduleLoadedCallback; - script.src = scriptUrl + ( scriptUrl.slice(-7) === '.dev.js' ? cacheSuffix : '' ); - script.onerror = function() { - $.formUtils.warn('Unable to load form validation module '+scriptUrl); - }; - script.onreadystatechange = function () { - // IE 7 fix - if (this.readyState === 'complete' || this.readyState === 'loaded') { - moduleLoadedCallback(); - // Handle memory leak in IE - this.onload = null; - this.onreadystatechange = null; - } - }; - appendToElement.appendChild(script); + if (typeof define === 'function' && define.amd) { + require([scriptUrl + ( scriptUrl.slice(-7) === '.dev.js' ? cacheSuffix : '' )], moduleLoadedCallback); + } else { + // Load the script + script.type = 'text/javascript'; + script.onload = moduleLoadedCallback; + script.src = scriptUrl + ( scriptUrl.slice(-7) === '.dev.js' ? cacheSuffix : '' ); + script.onerror = function() { + $.formUtils.warn('Unable to load form validation module '+scriptUrl); + }; + script.onreadystatechange = function () { + // IE 7 fix + if (this.readyState === 'complete' || this.readyState === 'loaded') { + moduleLoadedCallback(); + // Handle memory leak in IE + this.onload = null; + this.onreadystatechange = null; + } + }; + appendToElement.appendChild(script); + } } } }); diff --git a/src/main/module-loader.js b/src/main/module-loader.js index eb2d90a..3026d21 100644 --- a/src/main/module-loader.js +++ b/src/main/module-loader.js @@ -86,23 +86,27 @@ $.formUtils.loadedModules[scriptUrl] = 1; hasLoadedAnyModule = true; - // Load the script - script.type = 'text/javascript'; - script.onload = moduleLoadedCallback; - script.src = scriptUrl + ( scriptUrl.slice(-7) === '.dev.js' ? cacheSuffix : '' ); - script.onerror = function() { - $.formUtils.warn('Unable to load form validation module '+scriptUrl); - }; - script.onreadystatechange = function () { - // IE 7 fix - if (this.readyState === 'complete' || this.readyState === 'loaded') { - moduleLoadedCallback(); - // Handle memory leak in IE - this.onload = null; - this.onreadystatechange = null; - } - }; - appendToElement.appendChild(script); + if (typeof define === 'function' && define.amd) { + require([scriptUrl + ( scriptUrl.slice(-7) === '.dev.js' ? cacheSuffix : '' )], moduleLoadedCallback); + } else { + // Load the script + script.type = 'text/javascript'; + script.onload = moduleLoadedCallback; + script.src = scriptUrl + ( scriptUrl.slice(-7) === '.dev.js' ? cacheSuffix : '' ); + script.onerror = function() { + $.formUtils.warn('Unable to load form validation module '+scriptUrl); + }; + script.onreadystatechange = function () { + // IE 7 fix + if (this.readyState === 'complete' || this.readyState === 'loaded') { + moduleLoadedCallback(); + // Handle memory leak in IE + this.onload = null; + this.onreadystatechange = null; + } + }; + appendToElement.appendChild(script); + } } } });