tags:

views:

465

answers:

3

All i need to do is to execute a callback function when my current function execution end

function LoadData() 
{
    alert('the data have been loaded');
    //call my callback with parameters i want Ex.
    //callback(loadedData , currentObject);
}

Consumer for this function should be like this

object.LoadData(success);

function success(loadedData , currentObject) 
{
  //todo some action here 
}

So how to implement this

+7  A: 

Actually, your code will pretty much work as is, just declare your callback as an argument and you can call it directly using the argument name.

The basics

function doSomething(callback) {
    // ...

    // Call the callback
    callback('stuff', 'goes', 'here');
}

function foo(a, b, c) {
    // I'm the callback
    alert(a + " " + b + " " + c);
}

doSomething(foo);

That will call doSomething, which will call foo, which will alert "stuff goes here".

Note that it's very important to pass the function reference (foo), rather than calling the function and passing its result (foo()). In your question, you do it properly, but it's just worth pointing out because it's a common error.

More advanced stuff

Sometimes you want to call the callback in a specific context -- e.g., so the this value inside the callback has a specific value. You can easily do that with the JavaScript call function:

function Thing(name) {
    this.name = name;
}
Thing.prototype.doSomething = function(callback) {
    // Call our callback, but using our own instance as the context
    callback.call(this);
}

function foo() {
    alert(this.name);
}

var t = new Thing('Joe');
t.doSomething();  // Alerts "Joe" via `foo`

You can also pass parameters:

function Thing(name) {
    this.name = name;
}
Thing.prototype.doSomething = function(callback, salutation) {
    // Call our callback, but using our own instance as the context
    callback.call(this, salutation);
}

function foo(salutation) {
    alert(salutation + " " + this.name);
}

var t = new Thing('Joe');
t.doSomething('Hi;');  // Alerts "Hi Joe" via `foo`

Sometimes it's useful to pass the arguments you want to give the callback as an array, rather than individually. You can use apply to do that:

function Thing(name) {
    this.name = name;
}
Thing.prototype.doSomething = function(callback) {
    // Call our callback, but using our own instance as the context
    callback.apply(this, ['Hi', 3, 2, 1]);
}

function foo(salutation, three, two, one) {
    alert(salutation + " " + this.name + " - " + three + " " + two + " " + one);
}

var t = new Thing('Joe');
t.doSomething('Hi;');  // Alerts "Hi Joe - 3 2 1" via `foo`
T.J. Crowder
I know it will work if i don't have any parameters like the example you wrote but when i try to pass a function with parameters it's throwing an exception and telling me the function is not defined
Amgad Fahmi
@TiTaN: That's strange, there's nothing special about passing parameters into the callback. The callback reference you pass into your function is a function reference like any other, you can do all the normal things with it.
T.J. Crowder
@everyone who answered: I think TiTaN's problem is that he doesn't know how to pass a function that requires arguments into a callback that doesn't pass any arguments. Think `setTimeout()`. The answer is to wrap the callback in a closure: `doSomething(function(){foo('this','should','work')})`
slebetman
Someone point TiTaN to a thread (preferably on SO) discussing the issue above, my search-fu is weak today.
slebetman
A: 
function LoadData(callback) 
{
    alert('the data have been loaded');
    callback(loadedData, currentObject);
}
Andreas Bonini
callback is undefined
Amgad Fahmi
A: 

Have you tried:

function LoadData (callback)
{
    // ... process whatever data
    callback (loadedData, currentObject);
}

Functions are first class in Javascript, you can just pass them around

K Prime
It's not working keep telling me the callback is undefined
Amgad Fahmi