fuzzybabybunny
fuzzybabybunny

Reputation: 5254

How to invoke a client side method from the server in Meteor?

The reverse (calling server-side from the client) is easily done with a Meteor Method.

Say that you want to run a client-side method from the server, either directly or as a callback. An example could be that a user signs up on your site. After the sign-up on the server is successful, a modal window pops up alerting them that the sign up has been successful (we're trying to go for a single page app feel, so not doing a router re-direct).

Previous searches seem to say that I need to create a reactive collection and monitor it with observe() just for this purpose:

Invoke a client js function in Meteor after getting results from the server

This seems kind of hacky. Is there a better way to do this?

Upvotes: 3

Views: 323

Answers (1)

Donny Winston
Donny Winston

Reputation: 2452

For your example, invoking Accounts.createUser (docs) on the client would be a fine way to sign up a user and invoke a callback on success or error.

In general, you want client code to be conditional on data rather than on a particular server's state (you may one day have dozens of servers serving the same app!).

To run code on the client once if and when your data has a certain truth (e.g. Meteor.userId() is not null, so a user has logged in), you can use a Deps.autorun that stop()s itself (docs).

Upvotes: 3

Related Questions