doque
doque

Reputation: 2733

Extending done() in jQuery's Promise object

I'd like to wrap $.ajax().done() in a separate class, which includes validation of JSON responses against a schema.

A sample call could look like this:

myService.get("/api/people").done(function(people) {
    // at this point I'm certain the data in people is valid
    template.render(people);
}).catch(function() {
    // this happens if validation of people failed, even if the request itself was successfull
    console.log("empty json or validation failed");
});

The success callback function is passed in done(), but should only be executed if a private function (_validate(data, schema)) returns true. A less elegant version could look like this:

myService.get("api/people", successCallback, errorCallback);

I would like to expose the internal Deferred methods of $.ajax() directly. Is this possible?

Upvotes: 0

Views: 177

Answers (1)

Halcyon
Halcyon

Reputation: 57729

You don't need to change the Promises. You can use then to layer promises.

function _validate(data, schema) {
    return false;
}

var myService = {
    get: function (data) {
        return $.ajax(data).then(function (reply) {
            if (_validate(reply, schema)) {
                return reply;
            } else {
                // works if your library is Promises/A+ compliant (jQuery is not)
                throw new Error("data is not valid JSON"); // causes the promise to fail
                /*// else do:
                var d = new $.Deferred();
                d.reject("data is not valid JSON");
                return d.promise();*/
            }
        });
    }
}

myService.get("foo").done(function () { /* success */ }).fail(function () { /*failed */ });

Upvotes: 5

Related Questions