87

I'm trying to give my plugin callback functionality, and I'd like for it to operate in a somewhat traditional way:

myPlugin({options}, function() {
    /* code to execute */
});

or

myPlugin({options}, anotherFunction());

How do I handle that parameter in the code? Is it treated as one full entity? I'm pretty sure I know where I'd place the executory code, but how do I get the code to execute? I can't seem to find a lot of literature on the topic.

3
  • 2
    Your second syntax calls the function instead of passing it. You need to remove the () Commented Mar 28, 2010 at 20:40
  • 6
    Personally I think you're better off having the callback be specified as part of the "options" parameter. That's especially true if it develops that there are more reasons than one to supply a callback. Commented Mar 28, 2010 at 20:53
  • 1
    How would something like that look, Pointy? Care to provide an answer-answer? Commented Mar 28, 2010 at 21:10

6 Answers 6

169

Just execute the callback in the plugin:

$.fn.myPlugin = function(options, callback) {
    if (typeof callback == 'function') { // make sure the callback is a function
        callback.call(this); // brings the scope to the callback
    }
};

You can also have the callback in the options object:

$.fn.myPlugin = function() {

    // extend the options from pre-defined values:
    var options = $.extend({
        callback: function() {}
    }, arguments[0] || {});

    // call the callback and apply the scope:
    options.callback.call(this);

};

Use it like this:

$('.elem').myPlugin({
    callback: function() {
        // some action
    }
});
Sign up to request clarification or add additional context in comments.

4 Comments

Just an add for this answer. There is a post that explains this: jquery-howto.blogspot.com/2009/11/…
@David How to add callback parameter, I want to do this $('.elem').myPlugin({ callback: function (param) { // some action } });
@JeaffreyGilbert You should bring the jQuery context as well, I would do options.callback.call(this, param);
@David I want two callbacks, called animateBefore and animateAfter. please tell me how to use your solution ??
6

I don't know if I understand your question correctly. But for the second version: This would call anotherFunction immediately.

Basically your plugin should be some kind of function that looks like this:

var myPlugin = function(options, callback) {
    //do something with options here
    //call callback
    if(callback) callback();
} 

You have to provide a function object as callback, so either function(){...} or anotherFunction (without () ).

Comments

4

Bringing back a blast from the past.

Worth noting that if you have two arguments passed, for example:

$.fn.plugin = function(options, callback) { ... };

Then you call the plugin without the options argument but with a callback then you'll run into issues:

$(selector).plugin(function() {...});

I use this to make it a little more flexible:

if($.isFunction(options)) { callback = options }

Comments

3

I think this might help you

// Create closure.
(function( $ ) {
  
   // This is the easiest way to have default options.
 
        var settings = $.extend({
            // These are the defaults.
 
            onready: function(){},
 
            //Rest of the Settings goes here...
        }, options );
 
    // Plugin definition.
    $.fn.hilight = function( options ) {
 
        //Here's the Callback
        settings.onready.call(this);
 
        //Your plugin code goes Here
    };
  
// End of closure.
  
})( jQuery );

I had shared a article about Creating your Own jQuery Plugin.I think you should check that http://mycodingtricks.com/jquery/how-to-create-your-own-jquery-plugin/

Comments

1

Change your plugin function to take a second parameter. Assuming that the user passes a function, that parameter can be treated as a regular function.
Note that you can also make the callback a property of the options parameter.

For example:

$.fn.myPlugin = function(options, callback) {
    ...

    if(callback)        //If the caller supplied a callback
        callback(someParam);

    ...
});

Comments

0

An example bit late, but it can be useful. Using arguments can create the same functionality.

$.fn.myPlugin = function() {
    var el = $(this[0]);
    var args = arguments[0] || {};
    var callBack = arguments[1];
    .....
    if (typeof callback == 'function') {
        callback.call(this);
    }
}

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.