Meteor js:使Session对象部分持久化

时间:2013-10-27 11:59:49

标签: session meteor

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

我想要一种方法来获得'临时'会话密钥和'持久'会话密钥。我可以打电话给

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

我找到了a related post on SO,但这样可以保存这种方法,并且会保留整个Session对象,我不想这样做。另外,我不想使用MongoDB,我希望存储只是客户端......

非常感谢提前!

2 个答案:

答案 0 :(得分:10)

使用localStorage。如果你希望它被反应,这有点棘手,但我想你可以使用Session来完成localStorage

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

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

设置时:

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

除非你使用MongoDB或其他东西,否则一件事是不可能的,如果你在一个标签上设置它,在你刷新页面之前它不会改变其他标签。但我觉得Session无论如何都是这样的。

答案 1 :(得分:7)

我成功使用包u2622:persistent-session利用amplify.js本地存储功能,同时允许您使用Meteor Session对象设置和访问信息。

完成meteor add u2622:persistent-session后,您可以继续执行以下操作:

  • Session.setPersistent(key, value)
  • Session.setDefaultPersistent(key, value)
除非您手动清除浏览器的localStorage,否则

u2622:persistent-session会在标签和重新加载中维护您的数据。

查看Github page以获取更多信息