Connection.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * 在 ajax request 前修改url,增加服务端`basePath`
  3. * `basePath`在`app.json`配置
  4. */
  5. Ext.define('saas.override.data.Connection', {
  6. override: 'Ext.data.Connection',
  7. urlRegexp: /(http|ftp|https):\/\//,
  8. config: {
  9. /**
  10. * @cfg {Object} defaultServerHeaders
  11. * 与defaultHeaders有区别,只在调用server api的时候才添加的headers
  12. */
  13. defaultServerHeaders: null
  14. },
  15. privates: {
  16. parseBasePath: function(basePath) {
  17. if (Ext.isObject(basePath)) {
  18. var protocol = window.location.protocol.split(":")[0];
  19. if (protocol === 'file') {
  20. protocol = 'https';
  21. }
  22. return basePath[protocol];
  23. }
  24. return basePath;
  25. },
  26. setupServerOptions: function(options) {
  27. var serverOptions = Ext.manifest.server, originUrl = options.url;
  28. if (serverOptions && serverOptions.basePath && !this.urlRegexp.test(originUrl) &&
  29. (!serverOptions.urlPattern || new RegExp(serverOptions.urlPattern).test(originUrl))) {
  30. Ext.Object.merge(options, {
  31. url: this.parseBasePath(serverOptions.basePath)
  32. + (originUrl.indexOf('/') == 0 ? '' : '/') + originUrl,
  33. headers: this.getDefaultServerHeaders() || {}
  34. });
  35. }
  36. }
  37. },
  38. request: function(options) {
  39. if (options && options.url) {
  40. this.setupServerOptions(options);
  41. }
  42. return this.callParent([options]);
  43. }
  44. });