[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/resources/ -> Connector.js (source)

   1  /*+***********************************************************************************
   2   * The contents of this file are subject to the vtiger CRM Public License Version 1.0
   3   * ("License"); You may not use this file except in compliance with the License
   4   * The Original Code is:  vtiger CRM Open Source
   5   * The Initial Developer of the Original Code is vtiger.
   6   * Portions created by vtiger are Copyright (C) vtiger.
   7   * All Rights Reserved.
   8   *************************************************************************************/
   9  
  10  var AppConnector = {
  11  
  12      /**
  13       * Sends a pjax request (push state +ajax)
  14       * The function is deferred. it will be resolved on success and error on failure
  15       *  Success - if request is success it will send you data that it recieved
  16       *  error - it will send two parameters first gives string regarding error
  17       *                   Second gives you error object if exists
  18       *
  19       *  @return - deferred promise
  20       */
  21      requestPjax : function(params) {
  22          return AppConnector._request(params, true);
  23      },
  24  
  25      /**
  26       *  Sends ajax request to the specified url.
  27       *  The function is deferred. it will be resolved on success and error on failure
  28       *  Success - if request is success it will send you data that it recieved
  29       *  error - it will send two parameters first gives string regarding error
  30       *                   Second gives you error object if exists
  31       *
  32       *  @return - deferred promise
  33       */
  34      request : function(params) {
  35          return AppConnector._request(params, false);
  36      },
  37  
  38      
  39      _request : function(params, pjaxMode) {
  40          var aDeferred = jQuery.Deferred();
  41  
  42          if(typeof pjaxMode == 'undefined') {
  43              pjaxMode = false;
  44          }
  45  
  46          if(typeof params == 'undefined') params = {};
  47      
  48          //caller has send only data
  49          if(typeof params.data == 'undefined') {
  50              if(typeof params == 'string') {
  51                  var callerParams = params;
  52                  if(callerParams.indexOf('?')!== -1) {
  53                      var callerParamsParts = callerParams.split('?')
  54                      callerParams = callerParamsParts[1];
  55                  }
  56              }else{
  57                  callerParams = jQuery.extend({}, params);
  58              }
  59              params = {};
  60              params.data = callerParams;
  61          }
  62          //Make the request as post by default
  63          if(typeof params.type == 'undefined') params.type = 'POST';
  64  
  65          //By default we expect json from the server
  66          if(typeof params.dataType == 'undefined'){
  67              var data = params.data;
  68              //view will return html
  69              params.dataType='json';
  70              if(data.hasOwnProperty('view')){
  71                  params.dataType='html';
  72              }
  73              else if (typeof data == 'string' && data.indexOf('&view=') !== -1) {
  74                  params.dataType='html';
  75              }
  76              
  77              if(typeof params.url != 'undefined' && params.url.indexOf('&view=')!== -1) {
  78                  params.dataType='html';
  79              }
  80          }
  81  
  82          //If url contains params then seperate them and make them as data
  83          if(typeof params.url != 'undefined' && params.url.indexOf('?')!== -1) {
  84              var urlSplit = params.url.split('?');
  85              var queryString = urlSplit[1];
  86              params.url = urlSplit[0];
  87              var queryParameters = queryString.split('&');
  88              for(var index=0; index<queryParameters.length; index++) {
  89                  var queryParam = queryParameters[index];
  90                  var queryParamComponents = queryParam.split('=');
  91                  params.data[queryParamComponents[0]] = queryParamComponents[1];
  92              }
  93          }
  94  
  95          if(typeof params.url == 'undefined' ||  params.url.length <= 0){
  96               params.url = 'index.php';
  97          }
  98  
  99  
 100          var success = function(data,status,jqXHR) {
 101              aDeferred.resolve(data);
 102          }
 103  
 104          var error = function(jqXHR, textStatus, errorThrown){
 105              aDeferred.reject(textStatus, errorThrown);
 106          }
 107          
 108          if(pjaxMode) {
 109              if(typeof params.container == 'undefined') params.container = '#pjaxContainer';
 110  
 111              params.type = 'GET';
 112  
 113              var pjaxContainer = jQuery('#pjaxContainer');
 114              //Clear contents existing before
 115              if(params.container == '#pjaxContainer') {
 116                  pjaxContainer.html('');
 117              }
 118  
 119              jQuery(document).on('pjax:success', function(event, data,status,jqXHR){
 120                  pjaxContainer.html('');
 121                  success(data,status,jqXHR);
 122              })
 123              
 124              jQuery(document).on('pjax:error', function(event, jqXHR, textStatus, errorThrown){
 125                  pjaxContainer.html('');
 126                  error(jqXHR, textStatus, errorThrown);
 127              })
 128              jQuery.pjax(params);
 129  
 130          }else{
 131              params.success = success;
 132  
 133              params.error = error;
 134              jQuery.ajax(params);
 135          }
 136  
 137          return aDeferred.promise();
 138      }
 139  
 140  }
 141  


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1