diff --git a/e107_web/js/core/all.jquery.js b/e107_web/js/core/all.jquery.js index 5be2c2b8d..495e69171 100644 --- a/e107_web/js/core/all.jquery.js +++ b/e107_web/js/core/all.jquery.js @@ -146,7 +146,9 @@ var e107 = e107 || {'settings': {}, 'behaviors': {}}; // If this is a navigation controller, e.g. pager. nav: $element.attr('data-nav-inc'), // Old way - href='myscript.php#id-to-target. - href: $element.attr("href") + href: $element.attr("href"), + // Wait for final event. Useful for keyUp, keyDown... etc. + wait: $element.attr('data-event-wait') }; // If this is a navigation controller, e.g. pager. @@ -160,7 +162,16 @@ var e107 = e107 || {'settings': {}, 'behaviors': {}}; ajaxOptions.type = 'GET'; } - e107.callbacks.ajaxRequestHandler($element, ajaxOptions); + if(ajaxOptions.wait != null) + { + e107.callbacks.waitForFinalEvent(function(){ + e107.callbacks.ajaxRequestHandler($element, ajaxOptions); + }, parseInt(ajaxOptions.wait), event); + } + else + { + e107.callbacks.ajaxRequestHandler($element, ajaxOptions); + } return false; }); @@ -622,6 +633,39 @@ var e107 = e107 || {'settings': {}, 'behaviors': {}}; e107.attachBehaviors(); }; + /** + * Wait for final event. Useful when you need to call an event callback + * only once, but event is fired multiple times. For example: + * - resizing window manually + * - wait for User to stop typing + * + * Example usage: + * @code + * $(window).resize(function () { + * waitForFinalEvent(function(){ + * alert('Resize...'); + * //... + * }, 500, "some unique string"); + * }); + * @endcode + */ + e107.callbacks.waitForFinalEvent = (function () + { + var timers = {}; + return function (callback, ms, uniqueId) + { + if(!uniqueId) + { + uniqueId = "Don't call this twice without a uniqueId"; + } + if(timers[uniqueId]) + { + clearTimeout(timers[uniqueId]); + } + timers[uniqueId] = setTimeout(callback, ms); + }; + })(); + })(jQuery); $.ajaxSetup({