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
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, { 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
/** /**
@ -62,16 +62,13 @@ Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, {
* @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) {
delete this[component];
}, this);
this._execute('system.listMethods', { this._execute('system.listMethods', {
success: this._setMethods, success: this._setMethods,
scope: this scope: this
@ -82,14 +79,14 @@ Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, {
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',
@ -100,7 +97,7 @@ Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, {
options: options options: options
}); });
}, },
_onFailure: function(response, requestOptions) { _onFailure: function(response, requestOptions) {
var options = requestOptions.options; var options = requestOptions.options;
errorObj = { errorObj = {
@ -111,23 +108,23 @@ Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, {
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);
@ -143,21 +140,21 @@ Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, {
} }
} }
}, },
_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 {
@ -172,11 +169,11 @@ Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, {
_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);
@ -184,11 +181,15 @@ Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, {
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];
} }
Ext.each(this._components, function(component) {
if (!component in components) {
delete this[component];
}
}, this);
this._components = Ext.keys(components); this._components = Ext.keys(components);
this.fireEvent('connected', this); this.fireEvent('connected', this);
} }

View file

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

View file

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