barteloma
barteloma

Reputation: 6875

Creating dojo javascript function with callback

I have a dojo class like this.

    var widget = declare("app.util",null, {
        createSecuredLayers: function () {
           $.ajax.get({
                url: "/Api/GetLayer",
                success: function (e) {
                },
                error: function () {
                }
            });
        }
    });

I want to use this object with callback parameters. I mean I want to pass success and error callbacks as parameter.

    var util = new app.util();
    util.createSecuredLayers({
         success:function(){ },
         error:function(){ }
    });

Upvotes: 0

Views: 184

Answers (2)

springrolls
springrolls

Reputation: 1331

createSecuredLayers: function(item) {
    $.ajax.get({
        url: "/Api/GetLayer",
        success: item.successCallback,
        error: item.errorCallback
    });
}

When you call the method, don't forget to pass the response in the success callback.

util.createSecuredLayers({
    successCallback: function(resp) {},
    errorCallback: function(err) {}
});

Upvotes: 1

Woody
Woody

Reputation: 8022

You can do it like this:

var widget = declare("app.util",null, {
    createSecuredLayers: function (args) {
       $.ajax.get({
            url: "/Api/GetLayer",
            success: args.success,
            error: args.error
        });
    }
});


var util = new app.util();
util.createSecuredLayers({
     success:function(){ },
     error:function(){ }
});

You should also consider using Dojo's deferred

Upvotes: 1

Related Questions