2

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);
}
1
  • Without knowing where a user would get the item/pos/offset properties from, it's hard to give a solid answer. Could you do this with an object? Then the user could pass an element or a declared object? Commented Dec 7, 2013 at 0:29

1 Answer 1

4

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});
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.