将extjs作为变量传递时,extjs存储代理api引发错误,无法识别url

时间:2019-05-03 19:57:23

标签: extjs

我有一家商店,可从rest api提取数据。事实证明,如果我尝试将url作为变量传递,则会引发以下错误:

  

[E] Ext.data.proxy.Server.buildUrl():您正在使用ServerProxy,但未向其提供网址。

代码如下:

Ext.define('mystore', {
    extend: 'Ext.data.Store',
    alias: 'store.mystore',
    model: 'mymodel',
    restful: true,
    autoLoad: true,
    proxy: {
        type: 'ajax',
        headers: {
           'Accept': '*/*',
           'Cache-Control': 'no-cache',
           'Content-Type': 'application/json',
           'Authorization': localStorage.token
        },
        extraParams: {
           sort: 'clave',
           'filter[active]': true
        },

        reader: {
            type: 'json',
            rootProperty: 'data',
            successProperty: 'success'
        },

        writer: {
            type: 'json',
            writeAllFields: true,
            encode: true,
            rootProperty: 'data'
        },

        actionMethods: {
           read: 'GET'
        },

        api: {
           read: this.url,
           create: this.url,
           update: this.url,
           destroy: this.url
        },

        autoSave: true
    },
    constructor: function (config) {
        this.url = 'http://myurl...';
        console.log('>>>>>>>>>>>>>>>>>>>>>>>>',this.url);
        this.callParent(arguments);
        this.initConfig(config);
        return this;
    }
});

到目前为止,我已经阅读了构造函数应该首先部署,然后是组件。问题是我对如何解决这个问题一无所知,那么我该如何工作呢?我做错了什么方式?

2 个答案:

答案 0 :(得分:0)

您不能在this.url中使用Ext.define,因为this不引用创建的商店。您需要在构造函数中设置url或覆盖buildUrl方法。

示例:

constructor : function(config) {
    config = config || {};
    var me = this;
    this.url = '..';
    config.proxy = {
        api: {
              read: this.url,
              create: this.url,
              update: this.url,
              destroy: this.url
        }
    };
    this.callParent([config]);
}

答案 1 :(得分:0)

经过大量网上阅读和测试后,我自己找到了解决方案:( 要做的是构造商店,然后从调用视图的控制器传递url属性,即:

component.store = Ext.create('mystore', {
            url: 'part of the url'
        });

这样,当组件加载时,它已经知道url的值。希望这会有所帮助。