Tuesday, November 3, 2009

Create callback functions for your jQuery plugins & extensions

Most of the time custom jQuery plugins and extensions that we create do not use a callback functions. They usually simply work on DOM elements or do some calculations. But there are cases when we need to define our own custom callback functions for our plugins. And this is especially true when our plugins utilize AJAX querying.

Let’s say our custom jQuery extension gets data by making some AJAX request.

$.extend({
  myFunc : function(someArg){
    var url = "http://site.com?q=" + someArg;
    $.getJSON(url, function(data){

        // our function definition hardcoded

    });
  }
});

What is bad in this jQuery code is that the callback function is defined and hardcoded right in the plugin itself. The plugin is not really flexible and its user will have to change the plugin code, which is bad!

So the solution is to define your own custom callback function argument. Here is how it is done:

$.extend({
  myFunc : function(someArg, callbackFnk){
    var url = "http://site.com?q=" + someArg;
    $.getJSON(url, function(data){

      // now we are calling our own callback function
      if(typeof callbackFnk == 'function'){
        callbackFnk.call(this, data);
      }

    });
  }
});

$.myFunc(args, function(){
  // now my function is not hardcoded
  // in the plugin itself
});

The above code shows you how to create a callback function for AJAX load, but if you want to create a simple callback function for your jQuery plugin, for example the one that executes on your own custom events. To achive this you can do the following:

$.extend({
  myFunc : function(someArg, callbackFnk){
    // do something here
    var data = 'test';
 
    // now call a callback function
    if(typeof callbackFnk == 'function'){
      callbackFnk.call(this, data);
    }
  }
});

$.myFunc(someArg, function(arg){
  // now my function is not hardcoded
  // in the plugin itself
  // and arg = 'test'
});

7 comments:

  1. Hey, instead of if(typeof callbackFnk == 'function'), you can use $.isFunction

    ReplyDelete
  2. I gues you should callSince you're using jQuery I suppose it would be better to use isFunction() functionality to test whether the second argument is a function. And when you provide a function it could be useful to also provide its calling context/scope object.

    ReplyDelete
  3. It would probably be more robust to use jQuery.isFunction().

    ReplyDelete
  4. @Erik Zaadi, @Robert and Anonymous, Thanks for pointing that out. I completely forgot about $.isFunction()

    I should write couple of posts regarding this rarely used jQuery functions as $.isFunction() to remind fella programmers :)

    ReplyDelete
  5. It doesn't really matter. Both do the trick just fine. Nice post btw.

    ReplyDelete
  6. You are losing me on the actual syntax of building and passing the function argument.

    Nice example of theory, but unknown how this is implemented in the 'real world'.

    ReplyDelete
  7. Super nice Post, been looking for something like this. Thx

    ReplyDelete