Fix issues with js eventmanager. refs #2046

Fix plugin methods not being available when enabled until refresh. refs #2125
This commit is contained in:
Chase Sterling 2012-12-21 13:26:58 -05:00
parent 8f34e2abdb
commit e6267d9411
3 changed files with 38 additions and 34 deletions

View File

@ -40,20 +40,20 @@ Ext.namespace('Ext.ux.util');
Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, {
_components: [],
_methods: [],
_requests: {},
_url: null,
_optionKeys: ['scope', 'success', 'failure'],
constructor: function(config) {
Ext.ux.util.RpcClient.superclass.constructor.call(this, config);
this._url = config.url || null;
this._id = 0;
this.addEvents(
// raw events
/**
@ -62,16 +62,13 @@ Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, {
* @param {Ext.ux.util.RpcClient} this
*/
'connected',
'error'
);
this.reloadMethods();
},
reloadMethods: function() {
Ext.each(this._components, function(component) {
delete this[component];
}, this);
this._execute('system.listMethods', {
success: this._setMethods,
scope: this
@ -82,14 +79,14 @@ Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, {
options = options || {};
options.params = options.params || [];
options.id = this._id;
var request = Ext.encode({
method: method,
params: options.params,
id: options.id
});
this._id++;
return Ext.Ajax.request({
url: this._url,
method: 'POST',
@ -100,7 +97,7 @@ Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, {
options: options
});
},
_onFailure: function(response, requestOptions) {
var options = requestOptions.options;
errorObj = {
@ -111,23 +108,23 @@ Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, {
code: 255
}
}
this.fireEvent('error', errorObj, response, requestOptions)
if (Ext.type(options.failure) != 'function') return;
if (options.scope) {
options.failure.call(options.scope, errorObj, response, requestOptions);
} else {
options.failure(errorObj, response, requestOptions);
}
}
},
_onSuccess: function(response, requestOptions) {
var responseObj = Ext.decode(response.responseText);
var options = requestOptions.options;
if (responseObj.error) {
this.fireEvent('error', responseObj, response, requestOptions);
if (Ext.type(options.failure) != 'function') return;
if (options.scope) {
options.failure.call(options.scope, responseObj, response, requestOptions);
@ -143,21 +140,21 @@ Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, {
}
}
},
_parseArgs: function(args) {
var params = [];
Ext.each(args, function(arg) {
params.push(arg);
});
var options = params[params.length - 1];
if (Ext.type(options) == 'object') {
var keys = Ext.keys(options), isOption = false;
Ext.each(this._optionKeys, function(key) {
if (keys.indexOf(key) > -1) isOption = true;
});
if (isOption) {
params.remove(options)
} else {
@ -172,11 +169,11 @@ Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, {
_setMethods: function(methods) {
var components = {}, self = this;
Ext.each(methods, function(method) {
var parts = method.split('.');
var component = components[parts[0]] || {};
var fn = function() {
var options = self._parseArgs(arguments);
return self._execute(method, options);
@ -184,11 +181,15 @@ Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, {
component[parts[1]] = fn;
components[parts[0]] = component;
});
for (var name in components) {
self[name] = components[name];
}
Ext.each(this._components, function(component) {
if (!component in components) {
delete this[component];
}
}, this);
this._components = Ext.keys(components);
this.fireEvent('connected', this);
}

View File

@ -91,13 +91,15 @@ Deluge.EventsManager = Ext.extend(Ext.util.Observable, {
},
onGetEventsSuccess: function(events) {
if (!events) return;
Ext.each(events, function(event) {
var name = event[0], args = event[1];
args.splice(0, 0, name);
this.fireEvent.apply(this, args);
}, this);
if (this.running) this.getEvents();
if (!this.running) return;
if (events) {
Ext.each(events, function(event) {
var name = event[0], args = event[1];
args.splice(0, 0, name);
this.fireEvent.apply(this, args);
}, this);
}
this.getEvents();
},
// private

View File

@ -76,6 +76,7 @@ Deluge.Plugin = Ext.extend(Ext.util.Observable, {
* then executes the plugins setup method, onEnabled.
*/
enable: function() {
deluge.client.reloadMethods();
this.fireEvent("enable", this);
if (this.onEnable) this.onEnable();
},