Connection.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. return basePath[window.location.protocol.split(":")[0]];
  19. }
  20. return basePath;
  21. },
  22. setupServerOptions: function(options) {
  23. var serverOptions = Ext.manifest.server, originUrl = options.url;
  24. if (serverOptions && serverOptions.basePath && !this.urlRegexp.test(originUrl) &&
  25. (!serverOptions.urlPattern || new RegExp(serverOptions.urlPattern).test(originUrl))) {
  26. Ext.Object.merge(options, {
  27. url: this.parseBasePath(serverOptions.basePath)
  28. + (originUrl.indexOf('/') == 0 ? '' : '/') + originUrl,
  29. headers: this.getDefaultServerHeaders() || {}
  30. });
  31. }
  32. }
  33. },
  34. request: function(options) {
  35. if (options && options.url) {
  36. this.setupServerOptions(options);
  37. }
  38. return this.callParent([options]);
  39. }
  40. });