简体   繁体   中英

jQuery plugin callback function parameters

I am looking for a better way to access/manage data inside a plugin callback function. I want to do the same thing as the jQuery UI.

UI example:(how I want to do this) http://api.jqueryui.com/sortable/

$( ".selector" ).sortable({
   activate: function( event, ui ) {
     alert(ui.item)
     alert(ui.position)
     alert(ui.offset)
   }
});

my plugin example(how I now have it):

$( ".selector" ).myplugin({
   activate: function( event, item, postion, offset ) {//to much parameters
     alert(item)
     alert(position)
     alert(offset)
   }
});

//inside the plugin
var varItem = '';
var varPosition = '';
var varOffset = '';

if(typeof self.o.activate == 'function'){
    self.o.activate.call(this, varItem, varPosition, varOffset);
}

This should do the trick

$( ".selector" ).myplugin({
   activate: function( event, object ) {//to much parameters
     alert(object.item)
     alert(object.position)
     alert(object.offset)
   }
});

//inside the plugin
var varItem = '';
var varPosition = '';
var varOffset = '';

if(typeof self.o.activate == 'function'){
    self.o.activate.call(this, {item: varItem, position: varPosition, offset: varOffset});
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM