123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- Ext.define('Ext.ux.ajax.SimManager', {
- singleton: true,
- requires: [
- 'Ext.data.Connection',
- 'Ext.ux.ajax.SimXhr',
- 'Ext.ux.ajax.Simlet',
- 'Ext.ux.ajax.JsonSimlet'
- ],
-
-
- defaultType: 'basic',
-
- delay: 150,
-
- ready: false,
- constructor: function () {
- this.simlets = {};
- },
- getSimlet: function (url) {
-
- var me = this,
- index = url.indexOf('?');
- if (index < 0) {
- index = url.indexOf('#');
- }
- if (index > 0) {
- url = url.substring(0, index);
- }
- return me.simlets[url] || me.defaultSimlet;
- },
- getXhr: function (method, url, options, async) {
- var simlet = this.getSimlet(url);
- if (simlet) {
- return simlet.openRequest(method, url, options, async);
- }
- return null;
- },
-
- init: function (config) {
- var me = this;
- Ext.apply(me, config);
- if (!me.ready) {
- me.ready = true;
- if (!('defaultSimlet' in me)) {
- me.defaultSimlet = new Ext.ux.ajax.Simlet({
- status: 404,
- statusText: 'Not Found'
- });
- }
- me._openRequest = Ext.data.Connection.prototype.openRequest;
- Ext.data.Connection.override({
- openRequest: function (options, requestOptions, async) {
- var xhr = !options.nosim &&
- me.getXhr(requestOptions.method, requestOptions.url, options, async);
- if (!xhr) {
- xhr = this.callParent(arguments);
- }
- return xhr;
- }
- });
- if (Ext.data.JsonP) {
- Ext.data.JsonP.self.override({
- createScript: function (url, params, options) {
- var fullUrl = Ext.urlAppend(url, Ext.Object.toQueryString(params)),
- script = !options.nosim &&
- me.getXhr('GET', fullUrl, options, true);
- if (!script) {
- script = this.callParent(arguments);
- }
- return script;
- },
- loadScript: function (request) {
- var script = request.script;
- if (script.simlet) {
- script.jsonpCallback = request.params[request.callbackKey];
- script.send(null);
- } else {
- this.callParent(arguments);
- }
- }
- });
- }
- }
- return me;
- },
- openRequest: function (method, url, async) {
- var opt = {
- method: method,
- url: url
- };
- return this._openRequest.call(Ext.data.Connection.prototype, {}, opt, async);
- },
-
- register: function (simlet) {
- var me = this;
- me.init();
- function reg (one) {
- var simlet = one;
- if (!simlet.isSimlet) {
- simlet = Ext.create('simlet.' + (simlet.stype || me.defaultType), one);
- }
- me.simlets[one.url] = simlet;
- simlet.manager = me;
- }
- if (Ext.isArray(simlet)) {
- Ext.each(simlet, reg);
- } else if (simlet.isSimlet || simlet.url) {
- reg(simlet);
- } else {
- Ext.Object.each(simlet, function (url, s) {
- s.url = url;
- reg(s);
- });
- }
- return me;
- }
- });
|