change the formatting

This commit is contained in:
Damien Churchill 2010-01-26 17:42:17 +00:00
parent 7125eab8b2
commit 3e13fe1229

View File

@ -32,161 +32,165 @@ Copyright:
*/ */
Ext.namespace('Ext.ux.util'); Ext.namespace('Ext.ux.util');
(function() { /**
Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, { * A class that connects to a json-rpc resource and adds the available
* methods as functions to the class instance.
* @class Ext.ux.util.RpcClient
* @namespace Ext.ux.util
*/
Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, {
_components: [], _components: [],
_methods: [], _methods: [],
_requests: {}, _requests: {},
_url: null, _url: null,
_optionKeys: ['scope', 'success', 'failure'], _optionKeys: ['scope', 'success', 'failure'],
constructor: function(config) { constructor: function(config) {
Ext.ux.util.RpcClient.superclass.constructor.call(this, config); Ext.ux.util.RpcClient.superclass.constructor.call(this, config);
this._url = config.url || null; this._url = config.url || null;
this._id = 0; this._id = 0;
this.addEvents( this.addEvents(
// raw events // raw events
/** /**
* @event connected * @event connected
* Fires when the client has retrieved the list of methods from the server. * Fires when the client has retrieved the list of methods from the server.
* @param {Ext.ux.util.RpcClient} this * @param {Ext.ux.util.RpcClient} this
*/ */
'connected', 'connected',
'error' 'error'
); );
this.reloadMethods(); this.reloadMethods();
}, },
reloadMethods: function() { reloadMethods: function() {
Ext.each(this._components, function(component) { Ext.each(this._components, function(component) {
delete this[component]; delete this[component];
}, this); }, this);
this._execute('system.listMethods', { this._execute('system.listMethods', {
success: this._setMethods, success: this._setMethods,
scope: this scope: this
}); });
}, },
_execute: function(method, options) { _execute: function(method, options) {
options = options || {}; options = options || {};
options.params = options.params || []; options.params = options.params || [];
options.id = this._id; options.id = this._id;
var request = Ext.encode({ var request = Ext.encode({
method: method, method: method,
params: options.params, params: options.params,
id: options.id id: options.id
}); });
this._id++; this._id++;
return Ext.Ajax.request({ return Ext.Ajax.request({
url: this._url, url: this._url,
method: 'POST', method: 'POST',
success: this._onSuccess, success: this._onSuccess,
failure: this._onFailure, failure: this._onFailure,
scope: this, scope: this,
jsonData: request, jsonData: request,
options: options options: options
}); });
}, },
_onFailure: function(response, requestOptions) { _onFailure: function(response, requestOptions) {
var options = requestOptions.options; var options = requestOptions.options;
errorObj = { errorObj = {
id: options.id, id: options.id,
result: null, result: null,
error: { error: {
msg: 'HTTP: ' + response.status + ' ' + response.statusText, msg: 'HTTP: ' + response.status + ' ' + response.statusText,
code: 255 code: 255
} }
} }
this.fireEvent('error', errorObj, response, requestOptions) this.fireEvent('error', errorObj, response, requestOptions)
if (Ext.type(options.failure) != 'function') return; if (Ext.type(options.failure) != 'function') return;
if (options.scope) { if (options.scope) {
options.failure.call(options.scope, errorObj, response, requestOptions); options.failure.call(options.scope, errorObj, response, requestOptions);
} else { } else {
options.failure(errorObj, response, requestOptions); options.failure(errorObj, response, requestOptions);
} }
}, },
_onSuccess: function(response, requestOptions) { _onSuccess: function(response, requestOptions) {
var responseObj = Ext.decode(response.responseText); var responseObj = Ext.decode(response.responseText);
var options = requestOptions.options; var options = requestOptions.options;
if (responseObj.error) { if (responseObj.error) {
this.fireEvent('error', responseObj, response, requestOptions); this.fireEvent('error', responseObj, response, requestOptions);
if (Ext.type(options.failure) != 'function') return; if (Ext.type(options.failure) != 'function') return;
if (options.scope) { if (options.scope) {
options.failure.call(options.scope, responseObj, response, requestOptions); options.failure.call(options.scope, responseObj, response, requestOptions);
} else { } else {
options.failure(responseObj, response, requestOptions); options.failure(responseObj, response, requestOptions);
} }
} else { } else {
if (Ext.type(options.success) != 'function') return; if (Ext.type(options.success) != 'function') return;
if (options.scope) { if (options.scope) {
options.success.call(options.scope, responseObj.result, responseObj, response, requestOptions); options.success.call(options.scope, responseObj.result, responseObj, response, requestOptions);
} else { } else {
options.success(responseObj.result, responseObj, response, requestOptions); options.success(responseObj.result, responseObj, response, requestOptions);
} }
} }
}, },
_parseArgs: function(args) { _parseArgs: function(args) {
var params = []; var params = [];
Ext.each(args, function(arg) { Ext.each(args, function(arg) {
params.push(arg); params.push(arg);
}); });
var options = params[params.length - 1]; var options = params[params.length - 1];
if (Ext.type(options) == 'object') { if (Ext.type(options) == 'object') {
var keys = Ext.keys(options), isOption = false; var keys = Ext.keys(options), isOption = false;
Ext.each(this._optionKeys, function(key) { Ext.each(this._optionKeys, function(key) {
if (keys.indexOf(key) > -1) isOption = true; if (keys.indexOf(key) > -1) isOption = true;
}); });
if (isOption) { if (isOption) {
params.remove(options) params.remove(options)
} else { } else {
options = {} options = {}
} }
} else { } else {
options = {} options = {}
} }
options.params = params; options.params = params;
return options; return options;
}, },
_setMethods: function(methods) { _setMethods: function(methods) {
var components = {}, self = this; var components = {}, self = this;
Ext.each(methods, function(method) { Ext.each(methods, function(method) {
var parts = method.split('.'); var parts = method.split('.');
var component = components[parts[0]] || {}; var component = components[parts[0]] || {};
var fn = function() { var fn = function() {
var options = self._parseArgs(arguments); var options = self._parseArgs(arguments);
return self._execute(method, options); return self._execute(method, options);
} }
component[parts[1]] = fn; component[parts[1]] = fn;
components[parts[0]] = component; components[parts[0]] = component;
}); });
for (var name in components) { for (var name in components) {
self[name] = components[name]; self[name] = components[name];
} }
this._components = Ext.keys(components); this._components = Ext.keys(components);
this.fireEvent('connected', this); this.fireEvent('connected', this);
} }
}); });
})();