ExtJS:使用远程加载的单例值来定义存储

ExtJS: Using remotely loaded singleton values for store definition

本文关键字:单例值 存储 定义 加载 程加载 ExtJS      更新时间:2023-09-26

我有一些麻烦试图找出如何做到这一点(如果它甚至是可能的)。

我有一个应用程序使用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'实例时才会被解析。