ExtJS:使用远程加载的单例值来定义存储
ExtJS: Using remotely loaded singleton values for store definition
我有一些麻烦试图找出如何做到这一点(如果它甚至是可能的)。
我有一个应用程序使用parse.com来存储它的数据,问题是我希望每个用户都有一个不同的parse.com帐户,这样他们的数据集就不会相交。所以我创建了一个singleton(设置),它存储了用户的appId和apiKey,这是从一般的parse.com帐户加载的,由我管理,包含每个用户的电子邮件,appId和apiKey,所以当他们登录到应用程序时,它得到用户的appId和apiKey。
问题是我需要在商店的定义中使用这些设置,appId和apiKey,因为我需要在header中发送它们。我已经做了一些测试,试图在应用程序启动时设置我的单例全局变量,但在商店定义的时候,这两个"全局变量"都是空的,因为应用程序还没有启动。
下面是我的一些代码,这样我可以让自己更清楚一些,因为我知道这不是最容易理解的事情。
Application.js
Ext.define('Settings', {
singleton: true,
appId: null,
apiKey: null
});
Ext.define('MyApp.Application', {
extend: 'Ext.app.Application',
name: 'MyApp',
stores: [],
launch: function () {
Ext.create('MyApp.store.Settings').load({
params: {
'where': '{"email": "useremail@gmail.com"}' //email is supposed to be a user input but for the sakes of testing I just made it static
},
callback: function(records){
var s = records[0];
Settings.appId = s.get('appId');
Settings.apiKey = s.get('apiKey');
Parse.initialize(Settings.appId, Settings.apiKey);
}
});
},
onAppUpdate: function () {
Ext.Msg.confirm('Application Update', 'This application has an update, reload?',
function (choice) {
if (choice === 'yes') {
window.location.reload();
}
}
);
}
});
存储Ext.define('MyApp.store.Things', {
extend: 'Ext.data.Store',
model: 'MyApp.model.Thing',
proxy: {
type: 'rest',
api: {
read: 'https://api.parse.com/1/classes/Thing',
create: 'https://api.parse.com/1/classes/Thing'
},
reader: {
type: 'json',
rootProperty: 'results'
},
useDefaultXhrHeader: false,
withCredentials: false,
headers: {
'X-Parse-Application-Id': Settings.appId, //this is null at the time of definition, but I want it to be the newly fetched value at the time of app launch
'X-Parse-REST-API-Key': Settings.apiKey, //this is obviously null as well
'Content-Type': 'application/json'
}
},
autoLoad: true,
autoSync: true
});
怎么解决这个问题?
顺便说一下……如果有人能想到一个合适的名字,请随意更改或建议。
试试这样写:
Ext.define('Settings', {
singleton: true,
appId: null,
apiKey: null
});
Ext.define('MyApp.store.Things', {
extend: 'Ext.data.Store',
model: 'MyApp.model.Thing',
proxy: {
type: 'rest',
api: {
read: 'https://api.parse.com/1/classes/Thing',
create: 'https://api.parse.com/1/classes/Thing'
},
reader: {
type: 'json',
rootProperty: 'results'
},
useDefaultXhrHeader: false,
withCredentials: false,
},
//autoLoad: true,
autoSync: true
});
Ext.define('MyApp.Application', {
extend: 'Ext.app.Application',
name: 'MyApp',
stores: ['Things'],
launch: function() {
var settings = Ext.create('MyApp.store.Settings');
settings.on('load', function() {
var things = Ext.getStore('Things');
things.getProxy().setHeaders({
'X-Parse-Application-Id': Settings.appId,
'X-Parse-REST-API-Key': Settings.apiKey,
'Content-Type': 'application/json'
});
things.load();
});
settings.load({
params: {
'where': '{"email": "useremail@gmail.com"}' //email is supposed to be a user input but for the sakes of testing I just made it static
},
callback: function(records) {
var s = records[0];
Settings.appId = s.get('appId');
Settings.apiKey = s.get('apiKey');
Parse.initialize(Settings.appId, Settings.apiKey);
}
});
},
onAppUpdate: function() {
Ext.Msg.confirm('Application Update', 'This application has an update, reload?',
function(choice) {
if (choice === 'yes') {
window.location.reload();
}
}
);
}
});
我建议使用路由来完成此操作,因为您使用的是ExtJs 6。它完全是开箱即用的,但我想它对你的情况来说是最理想的。通过这种方式,你可以简单地确保,当一个路由被调用,你的应用程序的一部分被加载时,你总是可以做一些检查。例如,这对于检查用户凭据非常有用。更多关于路线的信息可以在这里找到。当你想通过路由处理用户会话时,这是一个很好的帖子。
单:
Ext.define('Settings', {
singleton: true,
appId: null,
apiKey: null
});
Base store:
Ext.define('Base', {
extend: 'Ext.data.Store',
alias: 'store.base',
storeId: 'base',
autoLoad: false,
proxy: {
type: 'rest',
useDefaultXhrHeader: false,
withCredentials: false
},
listeners: {
beforeload: function(store, operation, eOpts) {
store.getProxy().headers = {
'X-Parse-Application-Id': Settings.appId,
'X-Parse-REST-API-Key': Settings.apiKey,
'Content-Type': 'application/json'
}
}
}
});
The Things store:
Ext.define('MyApp.store.Things', {
extend: 'MyApp.store.Base',
alias: 'store.things',
model: 'MyApp.model.Thing',
storeId: 'things',
requires: [
'Settings'
],
proxy: {
api: {
read: 'https://api.parse.com/1/classes/Thing',
create: 'https://api.parse.com/1/classes/Thing'
},
reader: {
type: 'json',
rootProperty: 'results'
}
},
autoLoad: false, // --> set to false
autoSync: true
});
你MainController: Ext.define('MyApp.view.main.MainController', {
extend : 'Ext.app.ViewController',
requires: [
'Settings'
],
stores: [
'Things'
],
routes : {
'user/:id' : {
before : 'onBeforeUser',
action : 'onUser'
}
},
onBeforeUser : function(id, action) {
Ext.create('MyApp.store.Settings').load({
params: {
'where': '{"email": "useremail@gmail.com"}' //email is supposed to be a user input but for the sakes of testing I just made it static
},
callback: function(records){
var s = records[0];
Settings.appId = s.get('appId');
Settings.apiKey = s.get('apiKey');
Parse.initialize(Settings.appId, Settings.apiKey);
action.resume();
}
});
// or even better
Ext.Ajax.request({
url: 'url/to/the/api',
params: {
'where': '{"email": "useremail@gmail.com"}' //email is supposed to be a user input but for the sakes of testing I just made it static
},
success: function(response, opts) {
var obj = Ext.decode(response.responseText);
Settings.appId = obj.appId;
Settings.apiKey = obj.apiKey;
Parse.initialize(Settings.appId, Settings.apiKey);
action.resume();
},
failure: function(response, opts) {
action.stop(true);
}
});
},
onUser : function(id) {
Ext.getStore('things').load();
}
});
我认为这个问题可以通过将代理定义移动到'Things'存储的构造函数中来解决,如下所示。
Ext.define('MyApp.store.Things', {
extend: 'Ext.data.Store',
model: 'MyApp.model.Thing',
autoLoad: true,
autoSync: true,
constructor: function(config) {
config = Ext.apply({
proxy: {
type: 'rest',
api: {
read: 'https://api.parse.com/1/classes/Thing',
create: 'https://api.parse.com/1/classes/Thing'
},
reader: {
type: 'json',
rootProperty: 'results'
},
useDefaultXhrHeader: false,
withCredentials: false,
headers: {
'X-Parse-Application-Id': Settings.appId,
'X-Parse-REST-API-Key': Settings.appId,
'Content-Type': 'application/json'
}
}
}, config);
this.callParent([config]);
}
});
当代理定义在构造函数内时,appId和设置。apiKey只有在创建'MyApp.store.Things'实例时才会被解析。
- 如何在没有表单提交、没有js、没有jquery和没有ajax的情况下将输入的文本框值存储到php变量中
- 将布尔值存储在隐藏字段中以进行表单处理
- 原始值被称为单例的现象是什么?
- 将挖空单选按钮值的值存储在可观察数组中
- 将表单值存储在 Cookie 中
- 如何将表单中的值存储在变量中
- 单击按钮时将值存储在JSON中
- phonegap将单选按钮值存储到javascript中的sqlite
- 如何使用php或javascript单击添加按钮后动态添加选择框字段,并将所选值存储在数据库中的选定框中
- 单选按钮,并将其值存储到javascript变量中
- ExtJS:使用远程加载的单例值来定义存储
- 多个监听器vs返回每个人都需要的值的单例
- 如何通过单击按钮将从内容脚本检索到的值存储到文本框中
- 表单元素值没有存储在javascript变量中
- 将输入元素动态添加到表单中,并将值存储在数据库中
- Flux架构嵌套和非单例存储
- 如何根据单击将新值存储在具有现有值的变量中
- javascript表单元素值存储在数组中需要一点帮助
- 将HTML表中的每个选中单选按钮值存储到Jquery中的数组中
- 将选中的单选按钮值存储在Jquery数组中