Jonovono
Jonovono

Reputation: 3425

MeteorJS and AmplifyJS reactive

So I am using the amplifyjs package for meteor.

I am still fairly new to meteor and am having trouble making it a reactive context.

I am using the

amplify.store( string key )

So when I make changes to this or add a value I would like to have the view update reflecting that.

I think I will have to use Meteor.deps or autosubscribe but not sure where to start. Any help would be great.

Upvotes: 2

Views: 1072

Answers (2)

sebastien.b
sebastien.b

Reputation: 1047

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.

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

Upvotes: 1

cmather
cmather

Reputation: 1950

Since amplify is a third party package, it doesn't use Meteor contexts or have reactivity built in. But you could build your own reactive wrapper pretty easily. You can check out the deps docs to see how to use Meteor.deps.Context and Meteor.deps._ContextSet to do this:

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

I've also posted a video tutorial here: http://www.eventedmind.com/posts/reactivity-with-contexts

Upvotes: 4

Related Questions