在ExtJS的Rest URL中使用DataFields访问Context.io API

时间:2018-10-22 07:37:29

标签: rest extjs field

我对EXTJS中的Rest API有两个疑问。

  1. 如何使用字段使动态URL动态化?
  2. 如何在我的Rest.Proxy中添加身份验证密钥以访问Context.io?

这是我的解决方案,但是我不确定是否做得正确。我在ExtJS中还很陌生,所以我的问题可能是基本的,但感谢您的帮助。

Ext.define("EmailFolders", {
  extend: "Ext.data.Model",
  fields: ["id", "label"],

  proxy: {
    type: "rest",
    url: "lite/users/:" + id + "/email_accounts/:" + label + "/folders"
  },

  reader: {
    type: "json"
  },

  headers: {
    CONSUMER_KEY: "KEY FROM CONTEX.IO",
    CONSUMER_SECRET: "SECRET FROM CONTEXT.IO"
  }
});

1 个答案:

答案 0 :(得分:0)

您可以使用store.getProxy()使动态URL保持动态并在标头中传递身份验证密钥。代理有方法

  1. proxy.setUrl()设置url的值。
  2. proxy.setHeaders()设置标题的值。

您可以在这里运行fiddle

代码片段

Ext.application({
    name: 'Fiddle',

    launch: function () {
        let url = 'https://jsonplaceholder.typicode.com/users';
        // Set up a model to use in our Store
        Ext.define('User', {
            extend: 'Ext.data.Model',

            proxy: {
                type: 'ajax',
                reader: {
                    type: 'json',
                    rootProperty: ''
                }
            }

        });

        Ext.define('MyStore', {
            extend: 'Ext.data.Store',
            model: 'User',

            listeners: {
                beforeload: function (store) {
                    var proxy = store.getProxy();

                    //if you want, you can also set here url inside of beforeload
                    //proxy.setUrl(url);

                    /*
                     * You can use {proxy.setHeaders} to set the values from CONTEX.IO
                     * After ajax request see your request parameter in network analysis below 2 headers are passed in request header
                     */
                    proxy.setHeaders({
                        CONSUMER_KEY: "KEY FROM CONTEX.IO",
                        CONSUMER_SECRET: "SECRET FROM CONTEXT.IO"
                    });

                }
            }
        });

        let store = new MyStore();
        //Set the dynamic url here
        //This {url} will be dynamic whatever you want to pass
        store.getProxy().setUrl(url);

        store.load(function (data) {
            console.log(data);
            alert('Open console to see reposne..!')
        });

        /*
         You can also pass url inside of load funtion
        */
        new MyStore().load({
            url: url + '/' + 1,
            callback: function (data) {
                console.log(data);
            }
        });
    }
});