Meteor js:使会话对象部分持久化 [英] Meteor js : make Session object partially persistent

查看:36
本文介绍了Meteor js:使会话对象部分持久化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的流星应用程序中,我使用 Session 来存储有关用户活动的临时信息.我想使用 amplify.js 将此信息的某些部分保留到浏览器中,但不是全部.

In my meteor app I'm using Session to store temporary info about user activity. I'd like to persist some parts of this info to the browser with amplify.js, but not all of it.

我想要一种拥有临时"会话密钥和持久"会话密钥的方法.例如我可以打电话

I'd like a way to have 'temporary' Session keys and 'persistent' Session keys. Eg I could call

Session.set('persistent', 'this is persisted to browser memory');
Session.set('temporary', 'this will be erased on page reload, etc');

然后在页面重新加载后

Session.get('persistent'); // returns 'this is persisted to browser memory'
Session.get('temporary'); // returns undefined

我找到了 SO 上的相关帖子 但这节省了这种方法保留了整个 Session 对象,而我没有想要做.另外,我不想为此使用 MongoDB,我希望存储纯粹是客户端...

I found a related post on SO but this saves this approach persists the entire Session object, which I don't want to do. Also, I don't want to use MongoDB for this, I'd like the storage to be purely client side...

非常感谢!

推荐答案

使用 localStorage.如果您希望它具有反应性,这有点棘手,但我想您可以使用 SessionlocalStorage

Use localStorage. It's a little tricky if you want it to be reactive, but I guess you could use Session to get that done alongside localStorage

启动时从浏览器的 localStorage jar 中获取项目

When starting up get the item from your browser's localStorage jar

Meteor.startup(function() {
    Session.set("yourItem", localStorage.getItem("yourItem"));
});

设置时:

localStorage.setItem("yourItem", "yourValue");
Session.set("yourItem", localStorage.getItem("yourItem"));

除非您使用 MongoDB 或其他东西,否则不可能实现的一件事是,如果您在一个选项卡上设置它,则在您刷新页面之前它不会更改其他选项卡.但我猜 Session 反正就是这个样子.

One thing that's not possible unless you use MongoDB or something is if you set this on one tab, it wont change on the others until you refresh the page. But I guess Session is like this anyway.

这篇关于Meteor js:使会话对象部分持久化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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