【问题标题】:ExtJS4 store proxy url overrideExtJS4 存储代理 url 覆盖
【发布时间】:2011-10-06 03:15:53
【问题描述】:

我正在尝试通过更改代理 url(实际端点而不是参数)来重用商店。是否可以使用以下语法覆盖商店实例的代理 URL:

{
...some view config ...
store: Ext.create('MyApp.store.MyTasks',{proxy:{url:'task/my.json'}}),
}

如果代理已经在 Store 定义中定义好?

编辑:AbstractStore源代码设置代理如下方式

    if (Ext.isString(proxy)) {
        proxy = {
            type: proxy    
        };
    }

解决方案:store.getProxy().url = 'task/myMethod.json';

【问题讨论】:

    标签: extjs extjs4


    【解决方案1】:
    {
        ... some tab config ...
        store: Ext.create('MyApp.store.MyTasks'),
        listeners: {
            afterrender: function(tab) {
                tab.store.getProxy().url = 'task/myMethod.json'; //<--Saki magic :)
                tab.store.load();
            }
        }
    }
    

    http://www.sencha.com/forum/showthread.php?149809-Reusing-Store-by-changing-Proxy-URL

    【讨论】:

      【解决方案2】:

      创建商店时,您不能单独覆盖代理的 url。您将必须通过一个完整的代理。这是因为,库替换了整个代理!所以,你可以做的是:

      {
      ...some view config ...
      store: Ext.create('MyApp.store.MyTasks',{
                  proxy: {
                      type: 'ajax',
                      url : 'task/my.json',
                      reader: {
                          type: 'json',
                          root: 'rows'
                      }
                  }
              }),
      }
      

      现在另一种可能性是,在您拥有 store 实例之后更改端点。如果需要从不同的端点加载 store,可以使用 load 方法。

      store.load({url:'task/others.json'});
      

      因为,在您的情况下,您正在尝试重复使用商店,因此您可以传递整个代理。您商店的 (MyApp.store.MyTasks) 构造函数应该能够处理新配置并将其应用到商店...这是一个示例:

      constructor: function(config) {
      
          this.initConfig(config);
          this.callParent();
      } 
      

      【讨论】:

      • 感谢您的完整回答。我标记了它,因为它很有用。然而,我从 Saki 那里得到了一种我更喜欢的稍微不同的方式。请参阅下面的答案。
      • store.getProxy().url = 'task/myMethod.json';
      【解决方案3】:

      使用store.setProxy() 方法。 Link here:

      【讨论】:

      • 谢谢,但这需要完整的代理配置,我正在寻找对现有代理的覆盖。
      • 也可以是类型字符串。尝试仅配置您要更改的 url。
      • Varun,如果使用 String 参数会发生什么,请参阅上面的编辑。
      【解决方案4】:

      我有一个 BaseStore 用于存储默认设置。

      Ext.define('ATCOM.store.Shifts', {
          extend : 'ATCOM.store.BaseStore',
          model : 'ATCOM.model.Shift',
          constructor : function(config) {
              this.callParent([config]);
              this.proxy.api = {
                  create : 'shifts/create.json',
                  read : 'shifts/read.json',
                  update : 'shifts/update.json',
                  destroy : 'shifts/delete.json',
              };
          }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-06-05
        • 2018-02-26
        • 2012-07-10
        • 1970-01-01
        • 1970-01-01
        • 2014-11-09
        • 2019-06-24
        相关资源
        最近更新 更多