rest - 在 ExtJS 中使用 Rest URL 中的 DataFields 来访问 Context.io API
问题描述
我有两个关于 EXTJS 中的 Rest API 的问题。
- 如何使用字段来使 REST URL 动态化?
- 如何在我的 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"
}
});
解决方案
您可以使用store.getProxy()
使 REST URL 动态化并在标头中传递身份验证密钥。代理有方法
proxy.setUrl()
设置 url 的值。proxy.setHeaders()
设置标题的值。
你可以在这里检查工作小提琴
代码片段
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);
}
});
}
});
推荐阅读
- file - 在哪里可以找到有关不同音频格式的文件结构的信息?
- c++ - 使用未初始化的 varialbe(int) 进行以后的计算
- javascript - Discord.js 十六进制颜色不起作用,它只是变成灰色
- docker - Docker WSL2 在 4 个套接字 Windows 10 企业中总共有 144 个线程,但在 WSL2 中只有 64 个被识别
- android - Android 8+ 上的 StatFs
- excel - 使用 VBA 过滤和复制粘贴值
- docusignapi - Docusign iframe 未加载
- yocto - Yocto:找不到工具链,请设置ARCH_arm64_TOOLCHAIN_PREFIX
- swift - 如何在 Swift 中的 IF 语句中设置时间延迟?
- c# - 在 Blazor 服务器应用程序上为移动和桌面设备加载不同的布局组件