tags:

views:

3996

answers:

5

I have this jquery function

    function example(file, targetwidget, callback){

    $(targetwidget).load(file, {limit: 25}, function(){
        $("#widget_accordion").accordion({fillSpace: true});
    });
}

it's working ok when I do:

 example('http://example.com/', "#divid", callback);

but I want to send the callback function inside the (callback) variable i.e: instead of butting the $("#widget_accordion").accordion({fillSpace: true}); inside the callback function I want to send it:

 example('http://example.com/', "#divid", '$("#widget_accordion").accordion({fillSpace: true});');

and then the function must be something like:

function example(file, targetwidget, callback){

$(targetwidget).load(file, {limit: 25}, function(){
    callback;
});

but that's not working

Thanks in advance for your help

+1  A: 

you need to invoke the function inside the callback

function example(file, targetwidget, callback){

$(targetwidget).load(file, {limit: 25}, function(){
    callback();
});
redsquare
but callback = $("#widget_accordion").accordion({fillSpace: true}); I will try it
Bassel Safadi
you still need to invoke the function by calling it with ()
redsquare
assuming the callback is indeed a valid function!
redsquare
+1  A: 

This should work:

$(targetwidget).load(file, {limit: 25}, callback);
peirix
it's not working because the value inside callback is text form jquery point of view
Bassel Safadi
+1  A: 

well.. You do this way:

function example(file, targetwidget, callback){

$(targetwidget).load(file, {limit: 25}, function(){
    if(typeof(callback)==='function'){
     callback.call(this, 'other parameters');
    }
});

The paramenters of callback will help you to send data back to callback function. ATTENTION, don't forget the this keyword!

Ionut Staicu
I'm getting an error here: callback.call is not a function
Bassel Safadi
then... it seems that callback is really not a function :)
Ionut Staicu
+1  A: 

Since you are passing a text string you would need to eval it:

function example(file, targetwidget, callback){

    $(targetwidget).load(file, {limit: 25}, function(){
        eval(callback);
    });

Edit:

if you want to use call() you would have to do it like this:

function callback() {
    $("#widget_accordion").accordion({fillSpace: true});
}

function example(file, targetwidget, callback){

    $(targetwidget).load(file, {limit: 25}, function(){
        if(typeof(callback)==='function'){
            callback.call(this);
        }
    });

example('http://example.com/', "#divid", callback);

In order for call to work, you would have to pass a function and not a string. But by wrapping your jQuery expression inside of a function, it would be run when the function is called. Calling a function can be done with the .call() method.

googletorp
answered 3 mins agogoogletorp666●10 -> and we all know eval is evil!
peirix
It works but is there any better way to do that?
Bassel Safadi
Yeah, read my comment http://stackoverflow.com/questions/1153241/send-a-jquery-callback-function-inside-a-variable/1153272#1153272 . Just use call() instead of eval()
Ionut Staicu
it throw a call() is not defined error
Bassel Safadi
+3  A: 

To pass the callback around, the variable needs to be of type function. Any of these should work:

function example(file, targetwidget, callback) {
  $(targetwidget).load(file, {limit:25}, callback);
}

// Call it by providing the function parameter via inline anonymous function:
example('http://example.com/', "#divid", function() {
  $("#widget_accordion").accordion({fillSpace: true});
});

// Or, declare a function variable and pass that in:
var widgetCallback = function() {
  $("#widget_accordion").accordion({fillSpace: true});
};

example('http://example.com/', "#divid", widgetCallback);

// Or, declare the function normally and pass that in:
function widgetCallback() {
  $("#widget_accordion").accordion({fillSpace: true});
}

example('http://example.com/', "#divid", widgetCallback);
Dave Ward