首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当上下文更改时刷新除会话数据之外的所有数据

当上下文更改时刷新除会话数据之外的所有数据
EN

Stack Overflow用户
提问于 2014-07-18 15:05:32
回答 1查看 777关注 0票数 0

我正在编写一个小示例应用程序,它将在仪表板和表中向用户显示一些信息。

该应用程序正在使用ember-cli版本0.0.37编写。和ember.js版本1.5.1。

我使用ember-simple-authember-simple-auth-oauth2进行身份验证,使用自定义身份验证器和授权程序在适当的情况下将client_id、client_secret和access_token注入到请求中(我完全知道客户端秘密不应该在前端应用程序中,但这是一个仅供内部使用的示例应用程序,而不是我问题的主题)。

自定义授权程序还向请求中注入o参数,请求的值为组织id。API返回数据使用access_token和o参数返回与特定组织有关的数据。组织id存储在会话中。

因此,一旦我浏览了一个组织,我有一个下拉组件,允许我选择另一个组织。目前,这在ApplicationRoute上调用一个操作,它更新会话中的值,并清除所有非帐户或组织的模型的存储,因为这些模型是在应用程序级别使用的。

代码:

代码语言:javascript
复制
setSessionOrganisation: function(organisation_id) {
    var self = this;

    /**
     * Check if its the same organisation
     */
    if (this.get('session.organisation_id') == organisation_id) {
        return;
    }

    /**
     * Get the organisation for the id provided
     */
    this.store.find('organisation', organisation_id).then(
        function(org) {

            /**
             * Update the session details
             */
            self.set('session.organisation_id', org.get('id'));
            self.set('session.organisation_name', org.get('name'));

            /**
             * Get all types
             */
            var store = self.container.lookup('store:main');
            var types = [];
            var typeMaps = store.typeMaps;
            $.each(typeMaps, function(key) {
                if (typeMaps.hasOwnProperty(key)) {
                    var type = typeMaps[key].type.typeKey;
                    if (type != 'account' && type != 'organisation'){
                        types.push(typeMaps[key].type.typeKey);
                    }
                }
            });

            /**
             * Clear data for types
             */
            for (var i = 0; i < types.length; i++) {
                store.unloadAll(types[i]);
            };
        });
}

我觉得上面的代码有点麻烦,但我还没有找到另一种方法来返回当前存储中的模型类型。

当调用此操作并刷新数据时,我想刷新/重新加载当前路由。有些路由将是动态的,并使用对象ids作为动态段。这些路由将需要重定向到它们的列表路由。当导航到其他路由时,它们将缓慢地加载数据。

所以我的问题是:

  • 是否有比我上面所做的更好的方法来清除存储数据?
  • 如何触发路由重装?
  • 如果在具有动态段的路由中,如何重定向到父路由?

作为一个额外的问题,是否有任何危险的卸载所有数据的意见/路线,目前没有显示?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-22 16:03:08

@marcoow的大力帮助下,我达成了一个符合我标准的决议

根据对这个问题的评论,我们讨论了重新加载整个应用程序来刷新数据存储缓存。虽然这种方法确实有效,但浏览器完全重新加载页面时出现了闪烁现象,我更愿意只重新加载数据并让应用程序自行排序。如果调用App.reset(),也会出现此闪烁。

我探索了其他选项并找到了Ember.Route.refresh()方法。这似乎会导致嵌套路由在从应用程序路由调用时重新加载它们的模型。

在我的应用程序中,我在初始化器中扩展了存储,以便调用一个函数,该函数可以从存储区卸载存储中的所有记录,但也可以为存储中的每种类型的模型提供一个模型名称列表,以排除:

app/initializers/custom-store.js

代码语言:javascript
复制
import DS from 'ember-data';

var CustomStore = DS.Store.extend({
    /**
     * Extend the functionality in the store to unload all instances of each type
     * of data except those passed
     *
     * @param  {Array} exclusions List of model type names to exclude from data
     *                            unload
     */
    unloadAllExcept: function(exclusions) {

        var typeMaps = this.get('typeMaps');

        for (var type in typeMaps) {
            if (typeMaps.hasOwnProperty(type)) {

                var typeKey = (typeMaps[type].type && typeMaps[type].type.typeKey) ? typeMaps[type].type.typeKey : false;

                if (typeKey && exclusions.indexOf(typeKey) === -1) {
                    this.unloadAll(typeKey);
                }
            }
        }
    }
});


export
default {
    after: 'store',
    name: 'custom-store',

    initialize: function(container /*, app*/ ) {
        container.unregister('store:main');
        container.register('store:main', CustomStore);
    }
};

这是从app/routes/application.jssetSessionOrganisation Action中调用的

代码语言:javascript
复制
setSessionOrganisation: function(organisation_id) {
    var _this = this;

    /**
     * Check if its the same organisation
     */
    if (this.get('session.organisation_id') === organisation_id) {
        return;
    }

    /**
     * Get the organisation for the id provided
     */
    this.store.find('organisation', organisation_id)
        .then(
            function(org) {

                /**
                 * Update the session details
                 */
                _this.get('session')
                    .set('organisation_id', org.get('id'));
                _this.get('session')
                    .set('organisation_name', org.get('name'));

                /**
                 * Clean the local data cache of all data pertaining to
                 * the old organisation
                 */
                _this.store.unloadAllExcept(['account', 'organisation']);

                /**
                 * refresh the application route (and subsequently all
                 * child routes)
                 */
                _this.refresh();
            });
    }

作为脚注,这个解决方案并不满足我提出的第三个问题,但我已经意识到,这个问题与处理API的响应有关,必须在模型挂钩期间处理。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24828212

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档