HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Is there anything can be improved in this JavaScript module?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thiscanimprovedanythingjavascriptmodulethere

Problem

I am using RequireJS JavaScript library. I have the following JavaScript module define for dataservice:

define(['jquery'], function ($) {

    var callApi = function (url, type, dataType, data, callback) {
        $.ajax({
            url: url,
            type: type,
            data: data,
            dataType: dataType,
            contentType: "application/json",
            success: function (data) {
                callback(data);
            }
        });
    };

    var getData = function (url, dataType, data, callback) {
        callApi(url, 'GET', dataType, data, callback);
    };

    var postData = function (url, data, callback) {
        callApi(url, 'POST', 'json', data, callback)
    };

    return {
        getData: getData,
        postData: postData
    };

});


Is there anything which can be improved in this module?

Solution

I really like this code.

Still, 2 observations:

  • The code is awfully optimistic, no default error handling ?



  • success gets data, textStatus, and jqXHR. I would pass all 3 to the callback function.

Context

StackExchange Code Review Q#41570, answer score: 4

Revisions (0)

No revisions yet.