EasyUI Forum
November 05, 2025, 04:08:00 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Extend event with 'middleware' [solved]  (Read 7909 times)
devnull
Sr. Member
****
Posts: 431


View Profile
« 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:

Code:
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 ?
« Last Edit: December 12, 2014, 05:39:11 PM by devnull » Logged

-- Licensed User --
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #1 on: December 12, 2014, 07:54:05 AM »

To extend a new 'afterSuccess' event, you have to redefine the 'form' plugin.
Code:
(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);
Logged
devnull
Sr. Member
****
Posts: 431


View Profile
« Reply #2 on: December 12, 2014, 05:39:41 PM »

Thanks, your support really is outstanding.
Logged

-- Licensed User --
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!