ExtJS 4.2:在第二次调用时重置代理URL

时间:2016-05-20 08:05:02

标签: extjs proxy

如何在再次调用代理网址之前重置代理网址?我目前有这个:

var sUrl = oStore.getProxy().url;
var internalId = oMe.getEcommIdField().html;
sUrl = sUrl.replace('/' + internalId, '');
sUrl = sUrl.replace('/activities', '');
sUrl = sUrl + '/' + oMe.getEcommIdField().html + '/activities/';
oStore.getProxy().url = sUrl;
oStore.load({
  callback: function() {
    oMask.hide();
    // console.log('sUrl',sUrl);
  }
});

首次加载时,这会正确调用正确的网址:base_url/{internalId}/activities

但是当我尝试再次拨打电话时,它会保留之前的值并附加新的值,例如:base_url/{internalId}//{internalId}/activities

如何正确重用代理网址以分配新加载的网址?

3 个答案:

答案 0 :(得分:1)

一种方法是使用beforeload / beforesync商店事件。像这样:

听众:{

beforeload: function( store, operation, eOpts ){

    // Checks if you have to look for some condition ..
    if(typeof param != "undefined" )
    {
        var proxy = store .getProxy();
        proxy.url = "my url";
        proxy.extraParams = "my parameters";
        store.setProxy(proxy);
    }
    else
        console.log("Job Not Completed");
    }
}

答案 1 :(得分:1)

您可以将setUrl功能添加到代理:

Ext.define('Path.store.YourStore', {
    extend: 'Ext.data.Store',
    model: 'Path.model.YourModel',
    proxy: {
        type: 'rest',
        urlTpl: 'base_url/{internalId}/activities',
        reader: {
            type: 'json',
            root: 'activities',
        },
        setUrl: function (internalId) {
            this.url = this.urlTpl.replace('{internalId}', internalId);
        }
    }
});

然后像

一样使用它
var internalId = oMe.getEcommIdField().html;
oStore.getProxy().setUrl(internalId); // now url = base_url/current_internalId/activities
oStore.load({
  callback: function() {
  }
});

或者您可以在代理中更改整个网址:

  setUrl: function (url) {
            this.url = url;
        }


var internalId = oMe.getEcommIdField().html;
oStore.getProxy().setUrl('new_url/' + internalId + '/path'); // now url = new_url/current_internalId/path
oStore.load({
  callback: function() {
  }
});

答案 2 :(得分:0)

你做的方式是正确的。问题在于这个代码。

sUrl = sUrl + '/' + oMe.getEcommIdField().html + '/activities/';
/*
 * after this sUrl = 'base_url/{internalId}//{internalId}/activities/'
 */

在将网址分配给商店之前,请确保网址正确无误。将新网址分配给商店的常用方法就是您正在使用的方式。

store.getProxy().url = newUrl;
store.load();