2015년 4월 19일 일요일

Ext.data.proxy.Ajax Extend

Ext.define('Tequiol.store.JsonRpc', {
  requires: ['Tequiol.util.Md5', 'Tequiol.util.Base64'],
  extend:'Ext.data.proxy.Ajax',
  alias:'proxy.jsonrpc',

  reader:{
    type:'json',
    root:'result'
  },

  statics: {
    /**
     * @private
     * Stores authentication data.
     */
    authentication:'',

    /**
     * @public
     * Changes the authentication data and generates a new client id for the connection.
     * @param username to log in
     * @param password to log in
     */
    setAuth:function (username, password) {
      var clientId = Ext.create('Tequiol.util.Md5').checksum(Math.random());
      this.authentication = Ext.create('Tequiol.util.Base64').encode(clientId + '-' + username + ':' + password);
    },

    /**
     * @public
     * returns true, if authentication data is available (does not tell, if the user is logged in)
     */
    hasAuth:function () {
      return this.authentication != '';
    }
  },

  constructor:function (config) {
    config = config || {};
    this.url = config.url || '/json';
    this.method = config.method || '';
    this.params = config.params || {};
    this.callParent([config]);
  },

  /**
   * @private
   * Modified headers for authentication and adds JSON-RPC header to the data request.
   * @param operation
   */
  buildRequest:function(operation) {
    var request = this.callParent([operation]);

    this.headers = {
      'Authorization':'Basic ' + this.self.authentication,
      'Content-Type':'application/json; charset=UTF-8'
    };

    Ext.apply(request, {
      jsonData:{
        version:'1.1',
        method:this.method,
        params:this.params
      },
      params: {} // get rid of GET params
    });

    return request;
  },

  /**
   * @private
   * Method is always POST.
   * @param request
   */
  getMethod: function(request) {
    return 'POST';
  }
});

댓글 없음:

댓글 쓰기