Jx.JxHttpProxyRest

This class provides the framework for applications to interact with REST Web services provided by the platform. This class overrides the doRequest method of Ext.data.HttpProxy to convert between the EXT JS style JsonData and the JsonData that can be sent over the wire to the REST service.


/**
 * @class Jx.JxHttpProxyRest
 * This is a class which should be used by apps to interract with the Restful web services
 * provided by the platform.
 */
Jx.JxHttpProxyRest = Ext.extend(Ext.data.HttpProxy, {
    // override constructor to copy the url from the top level to the api action method, if it's not already defined
    constructor: function (options) {
        Jx.JxHttpProxyRest.superclass.constructor.call(this, options);
        for (var verb in this.api) {
            var actobj = this.api[verb];
            if (!actobj.url) {
                actobj.url = this.url; // use default
            }
        }
    },
    doRequest: function (action, rs, params, reader, cb, scope, arg) {
        var o = {
            method: (this.api[action]) ? this.api[action]['method'] : undefined,
            request: {
                callback: cb,
                scope: scope,
                arg: arg
            },
            reader: reader,
            callback: this.createCallback(action, rs),
            scope: this,
            headers: {}
        };
        // copy either the top-level headers or the headers defined in the api action object
        Ext.apply(o.headers, this.headers ? this.headers : {});
        Ext.apply(o.headers, (this.api[action]) ? this.api[action]['headers'] : {});
        // Always do this.
        if (params.jsonData) {
            var jsonData = params.jsonData;
            o.jsonData = {};
            for (var i in jsonData) {
                var name = i.substring(i.lastIndexOf(".") + 1);
                o.jsonData[name] = jsonData[i];
            }
        } else if (params.xmlData) {
            o.xmlData = params.xmlData;
        } else {
            o.params = params || {};
        }
        // Set the connection url.  If this.conn.url is not null here,
        // the user may have overridden the url during a beforeaction event-handler.
        // this.conn.url is nullified after each request.
        if (this.conn.url === null) {
            this.conn.url = this.buildUrl(action, rs);
        } else if (this.restful === true && rs instanceof Ext.data.Record && !rs.phantom) {
            this.conn.url += '/' + rs.id;
        }
        if (this.useAjax) {
            Ext.applyIf(o, this.conn);
            // If a currently running request is found for this action, abort it.
            if (this.activeRequest[action]) {
                // Disabled aborting activeRequest while implementing REST.  activeRequest[action] will have to become an array
                //Ext.Ajax.abort(this.activeRequest[action]);
            }
            this.activeRequest[action] = Ext.Ajax.request(o);
        } else {
            this.conn.request(o);
        }
        // request is sent, nullify the connection url in preparation for the next request
        this.conn.url = null;
    }
});
// The following code should be in some sort of utility file. Placed here
// due to no other better choice.
//
// Over here we make sure every connection request will be injected with SDK
// specific headers. Possible header setting check can be performed here as well.
//
Ext.data.Connection.prototype.__request = Ext.data.Connection.prototype.request;
Ext.data.Connection.prototype.request = function (options) {
    // setup timezone. JxTimeKeeperModel.js has to be included prior
    // sample header set 'x-date: PDT,-700,GMT,-200,en-US'
    if (window.Jx.JxTimeKeeperModel) {
        // make sure library is there
        var headers = {
            'x-date': Jx.JxTimeKeeperModel.getTZStringForTimeOfYear(true, true, false) + ',' + Jx.JxTimeKeeperModel.getTZOffsetStringForTimeOfYear(true, false) + ',' + Jx.JxTimeKeeperModel.getTZStringForTimeOfYear(false, true, false) + ',' + Jx.JxTimeKeeperModel.getTZOffsetStringForTimeOfYear(false, false) + ',' + Jx.JxTimeKeeperModel.getLocale()
        };
        if (options.headers) {
            Ext.apply(options.headers, headers);
        } else {
            options.headers = headers;
        }
    }
    return this.__request(options);
};