最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Meteor Session and browser refreshes - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Nov 14, 2012 at 2:28 Jonovono asked Nov 14, 2012 at 0:35 JonovonoJonovono 3,4255 gold badges46 silver badges64 bronze badges 2
  • I save some common data in Meteor.user, When page refresh u can set it back. – crapthings Commented Nov 14, 2012 at 9:42
  • 2 Yeah, it's just I'd like persistance even when they are not logged in. – Jonovono Commented Nov 14, 2012 at 18:51
Add a comment  | 

5 Answers 5

Reset to default 10

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);
  },
});

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().

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

This is an old question, but it's the second hit on a search for "meteor session manager", so I think it's important to add that the package u2622:persistent-session solves this issue perfectly.

from the docs at: https://atmospherejs.com/u2622/persistent-session

Installation

meteor add u2622:persistent-session

That's it! Now you can use Session.setPersistent to set a session variable that will save after a refresh.

If you'd like, you can have Session.set do this as well. See the Options section below.

I've just created the UserSession package for Atmosphere, which is basically a user-based persistent Session.

So I think the best way to do this is to use the amplifyJS package.

Here is someones gist that shows how they used it: https://gist.github.com/2865146

The Session is a global key value store, a reactive one.

http://docs.meteor.com/#session

Session provides a global object on the client that you can use to store an arbitrary set of key-value pairs. Use it to store things like the currently selected item in a list.

What's special about Session is that it's reactive. If you call Session.get("currentList") from inside a template, the template will automatically be rerendered whenever Session.set("currentList", x) is called.

If you have to persist something in the client, you could use the browsers localstorage or cookies if you care for older browsers.

发布评论

评论列表(0)

  1. 暂无评论