Title: Extend event with 'middleware' [solved]
Post by: devnull on December 12, 2014, 02:23:42 AM
Hi; For a few controls such as combobox, form, linkbuttons, I need to be able to process an event firstly for all objects (onLoadSuccess) with some 'middleware' and then afterwards allow individual elements to optionally catch the original event and do some local processing (afterSuccess). This is what I have tried, but it does not appear to work: var onLoadSuccess = $.fn.form.methods.onLoadSuccess; $.extend($.fn.form.methods, { onLoadSuccess: function(jq){ return jq.each(function(){ onLoadSuccess.call($.fn.form.methods, $(this)); var opts = $(this).form('options'); if (opts.afterSuccess){ opts.afterSuccess.call(this);} }) } })
$.extend($.fn.form.defaults, { onLoadSuccess:function(jq){ console.log('always do....'); } })
$('#ff).form({ afterSuccess: function(){ console.log('do if the element wants to..'); } })
How can I achieve this ?
Title: Re: Extend event with 'middleware'
Post by: stworthy on December 12, 2014, 07:54:05 AM
To extend a new 'afterSuccess' event, you have to redefine the 'form' plugin. (function($){ var plugin = $.fn.form; $.fn.form = function(options, param){ if (typeof options == 'string'){ return plugin.call(this, options, param); } else { return this.each(function(){ plugin.call($(this), options, param); var opts = $(this).form('options'); var onLoadSuccess = opts.onLoadSuccess; opts.onLoadSuccess = function(data){ $.fn.form.defaults.onLoadSuccess.call(this, data); onLoadSuccess.call(this, data); if (opts.afterSuccess){ opts.afterSuccess.call(this); } } }); } }; $.fn.form.methods = plugin.methods; $.fn.form.defaults = plugin.defaults; $.fn.form.parseOptions = plugin.parseOptions; })(jQuery);
Title: Re: Extend event with 'middleware' [solved]
Post by: devnull on December 12, 2014, 05:39:41 PM
Thanks, your support really is outstanding.
|