Meteor Session 和浏览器刷新 [英] Meteor Session and browser refreshes

查看:20
本文介绍了Meteor Session 和浏览器刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Meteor 会话是否在页面刷新时重置?

Do Meteor sessions get reset when there is a page refresh?

出于某种原因,我认为他们没有,但似乎确实如此.有没有办法让他们坚持下去?

For some reason I did not think they did but it seems like they do. Is there a way to make them persist?

如果不是,最好的解决方案是什么?

If not what would be the best solution for this?

即使用户尚未注册,我也希望在用户刷新时显示相同的数据(此数据特定于用户).

I want to allow the same data to show if a user refreshes (this data is specific to the user) even if they are not registered yet.

推荐答案

实际上,您可以做的是创建一个 Session 的子类",在调用 set() 时将值存储在 Amplify 的存储中.您将自动继承 Session 的所有反应性属性.这是代码,它对我有用:

Actually what you could do is create a "subclass" of Session that stores the value in Amplify's store when set() is called. You would automatically inherit all the reactive properties of Session. Here is the code, it worked for me:

SessionAmplify = _.extend({}, Session, {
  keys: _.object(_.map(amplify.store(), function(value, key) {
    return [key, JSON.stringify(value)]
  })),
  set: function (key, value) {
    Session.set.apply(this, arguments);
    amplify.store(key, value);
  },
});

只需将所有 Session.set/get 调用替换为 SessionAmplify.set/get 调用即可.调用 set() 时,会调用父 Session 方法以及 amplify.store().当第一次创建子类"时,它会在其键中加载 amplify 存储中的所有内容,以便可以立即使用 get() 检索它们.

Just replace all your Session.set/get calls with SessionAmplify.set/get calls. When set() is called, the parent Session method is called, as well as amplify.store(). When the "subclass" is first created, it loads everything that is in amplify's store inside its keys, so that they can be retrieved right away with get().

您可以在此处测试排行榜示例的工作变体:https://github.com/sebastienbarre/流星排行榜

You can test a working variation of the Leaderboard example here: https://github.com/sebastienbarre/meteor-leaderboard

这篇关于Meteor Session 和浏览器刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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