ExtJS:使用远程加载的单例值进行存储定义 [英] ExtJS: Using remotely loaded singleton values for store definition

查看:126
本文介绍了ExtJS:使用远程加载的单例值进行存储定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些麻烦,想弄清楚如何做到这一点(如果可能的话)。

I'm having some trouble trying to figure out how to do this (if it's even possible).

我有一个应用程序,使用parse.com来存储这是数据,我希望每个用户有一个不同的parse.com帐户,所以他们的数据集不相交。所以我创建了一个单一的(设置),它存储用户的appId和apiKey,它们是由我管理的一般的parse.com帐户加载的,并包含每个用户的电子邮件,appId和apiKey,所以当他们登录到应用程序时用户的appId和apiKey。

I have an app which uses parse.com to store it's data, the thing is I want each user to have a different parse.com account so their data sets don't intersect whatsoever. So I created a singleton (Settings) which stores the user's appId and apiKey, which are loaded from a general parse.com account which is managed by me and contains each user's email, appId and apiKey, so when they log into the app it gets the user's appId and apiKey.

我需要在我的商店的定义中使用这些设置appId和apiKey,因为我需要将它们发送到头。我做了一些测试,试图在应用启动时设置我的单例全局变量,但是在商店定义的时候,这些全局变量都是空的,因为应用尚未启动。

The thing is I need to use those settings, appId and apiKey, in the definitions of my stores, as I need to send them in the headers. I've done some testing trying to set my singleton's globals when the app launchs, but at the time of the stores definition both of those "globals" are null, as the app hasn't launched yet.

这是我的一些代码,所以我可以让自己更清楚一些,因为我知道这不是最简单的事情。

Here's some of my code so I can make myself a little clearer as I know this isn't the easiest thing to understand.

应用程序.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
});

这是什么?

顺便说一句,如果有人能想到这个线索的正确名称,请随时更改或建议。

By the way.. if someone can think of a proper name for this thread please feel free to change it or suggest.

推荐答案

我认为这个问题可以通过将代理定义移动到下面给出的事物存储的构造函数来解决。

I think the issue can be solved by moving proxy definition to constructor of 'Things' store as given below.

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

当代理定义在构造函数内部时,Settings.appId和Settings.apiKey仅在当前解析实例创建MyApp.store.Things。

When proxy definition is inside the constructor, Settings.appId and Settings.apiKey are resolved only at the time of instance creation of 'MyApp.store.Things'.

这篇关于ExtJS:使用远程加载的单例值进行存储定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆