William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

jQuery.extend & callback

jQuery.extend & callback

Here is my simple example to use jQuery.extend to add params and callback function.

jQuery.fn.threeColumns = function(params, callback) {
  var opts = {
    fsize_step: 2,
    //will use for element.css(opts.adjust_box());
    adjust_box: function() {
      return {
        'margin': '20px',
        'padding': '20px',
        'height': 'auto'
      }
    }
  };
  jQuery.extend(opts, params);

  if(typeof opts.adjust_box === 'function') {
    console.log(opts.adjust_box());
  }
  callback(opts);
}

$('div.container').threeColumns({a:'b'}, function(params){
  console.log(params);
});

//Will display:
Object { margin="20px", padding="20px", height="auto"}
Object { fsize_step=2, a="b", adjust_box=function()}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: