extjs store proxy api throws error does not recognize url when passing it as a variable

三世轮回 提交于 2019-12-11 14:56:17

问题


I have a store which fetches data from rest api. Turns up that if I try to pass the url as a variable it throws me the following error:

[E] Ext.data.proxy.Server.buildUrl(): You are using a ServerProxy but have not supplied it with a url.

Here is the code:

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;
    }
});

So far I've read the constructor should be deployed in the very first place, and then the component. The thing is I have no idea on how to solve this, so how can I get this working? What am I doing the wrong way?


回答1:


You can't use this.url in Ext.define because this don't refer to created store. You need to set url in constructor or override the buildUrl method.

Example :

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]);
}



回答2:


I've found a solution myself, after a lot of reading in web foros and testing :( The thing is to construct the store and then pass url attrib from the controller which calls the view, that is to say:

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

like this, when the component loads it knows already the value of url. Hope this might help.



来源:https://stackoverflow.com/questions/55976344/extjs-store-proxy-api-throws-error-does-not-recognize-url-when-passing-it-as-a-v

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!