I'm having trouble trying to figure out how to do this (if possible).
I have an application that uses parse.com to store data, I want each user to have a different parse.com account, so their datasets do not overlap at all. So I created singleton (Settings), which stores the user appId and apiKey, which are downloaded from the general parse.com account, which is managed by me, and contains each user email address, appId and apiKey, so when they enter the application, it gets user appId and apiKey.
The thing is, I need to use these settings, appId and apiKey, in the definitions of my stores, since I need to send them to the headers. I did some testing trying to set my singleton globals when the application starts, but during the store definition both of these βglobalsβ are equal to zero, because the application is not running yet.
Here are some of my code, so I can make myself a little clearer, since I know that this is not the easiest thing to understand.
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(); } } ); } });
Score
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 });
How does this happen?
By the way, if someone can think of their own name for this topic, feel free to change it or suggest it.