AppSuite:Http.js

From Open-Xchange
Revision as of 14:21, 19 April 2013 by Frank.paczynski (talk | contribs) (Created page with "<div class="title">Requesting server api with http.js</div> '''Abstract''' * located: ''io.ox/core/http.js'' * purpose: centralizes ajax api calls __TOC__ = HTTP facades = ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Requesting server api with http.js

Abstract

  • located: io.ox/core/http.js
  • purpose: centralizes ajax api calls

HTTP facades

general example

http.GET({ 
        module: 'mail',
        params: { 
            action: 'all', 
            folder: 'default0/INBOX' 
        }
    });

GET(options)

/**
 * Send a GET request
 * @param {Object}  options Request options 
 * @returns {Object} jQuery's Deferred
 */

POST(options)

/**
 * Send a POST request
 * @param {Object} options Request options
 * @returns {Object} jQuery's Deferred
 */

FORM(options)

/**
 * Send a POST request using a FormData object
 * @param {Object} options Request options
 * @param {string} options.module Module, e.g. folder, mail, calendar etc.
 * @param {Object} options.params URL parameters
 * @returns {Object} jQuery's Deferred
 */

PUT(options)

/**
 * Send a PUT request
 * @param {Object} options Request options
 * @returns {Object} jQuery's Deferred
 */

DELETE(options)

/**
 * Send a DELETE request
 * @param {Object} options Request options
 * @returns {Object} jQuery's Deferred
 */

UPLOAD(options)

/**
 * Send a UPLOAD request
 * @param {Object} options Request options
 * @returns {Object} jQuery's Deferred
 */

column mappings

  • server requests
    • still require use of columns ids
  • server response
    • column_id keys will be replaced with column names to ease handling

getMappedModules()

/**
 * get names of mapped modules
 * @returns {array} mapped modules
 */

getAllColumns(module, join)

/**
 * get all columns of a module 
 * @param {string} module (name) 
 * @param {boolean} join (join array with comma separator ) 
 * @return {arrray|string} ids */ ```

getColumnMapping(module)

/**
 * returns the column mapping of a module
 * @param {string} module The module name.
 * @returns {object} A map from numeric column IDs to the corresponding field names.
 */

makeObject(data, module, columns)

/**
 * transform objects with array-based columns into key-value-based columns
 * @param {Array} data Data
 * @param {string} module Module name
 * @param {Array} columns Columns
 * @returns {Object} Transformed object
 */
/**
 * Fixes order of list requests (temp. fixes backend bug)
 * @param  {array} ids
 * @param  {deferred} deferred
 * @return {deferred} resolve returns array
 */

request stacking

  • stack ability for calls to minimize overhead of server communication

pause() and resume()

example

// pause http layer
http.pause();

// process all updates
_(list).map(function (item) {
    return http.PUT({
        module: 'calendar',
        params: {
            action: 'update',
            id: item.id,
            folder: item.folder_id,
            timestamp: item.timestamp
        },
        data: {  ...  },
    });
});

// resume & trigger refresh
http.resume()

retry (request)

  • retry request

utils

simplify(list)

  • simplify objects in array for list requests
  • returns array of items
  • possible returned item types
    • { id: '8978989' }
    • { folder: 'inbox' }
    • { recurrence_position: 'inbox' }
    • 8978989
    • 'inbox'
/**
 * Simplify objects in array for list requests
 * @param  {array} list
 * @returns {array} list    
 */

fixList(ids, deferred)

logging

log()

/**
 * returns failed calls
 * @return {backbone.collection}
 */