Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure to import methods into /imports/startup/client/index.js #53

Closed
tafelito opened this issue Sep 14, 2017 · 4 comments
Closed

Make sure to import methods into /imports/startup/client/index.js #53

tafelito opened this issue Sep 14, 2017 · 4 comments
Assignees
Labels

Comments

@tafelito
Copy link

I found that methods declared in imports/api/<module>/methods.js (e.g.: documents) are not being called from the client. This is because they are not being imported anywhere from the client side. They are imported from the server though. So the Optimistic UI feature won't work in that case.
One way to solve this is importing methods that are not inside server directories within startup/client/index.js or create methods using mdg:validated-method and then just importing the method to call from the component itself or wherever you use the method.call

Better explained here meteor/guide#491

@cleverbeagle
Copy link
Owner

This is one to explore further (cc @pilarArr). As you've explained, yes, the methods we've defined are server-side only. I've wanted to avoid validated methods as they add a layer of abstraction that can be difficult to understand for beginners (and adds a ton of "but wait, what is..." type of questions to the mix).

I've never investigated as I haven't had any trouble with server-only methods, but can we do a simple import '/imports/api/Documents/methods.js' in /imports/startup/client/index.js` for example? If so, this would be fine by me.

@tafelito
Copy link
Author

tafelito commented Sep 18, 2017

yes you are right, trying to understand the logic behind validated methods could be hard for beginners, even more if you never created a regular meteor method before.

if you want to have client-only or shared methods, you either create the methods.js file under imports/api/<module> or imports/api/<module>/client and then you can import it in /imports/startup/client/index.js. You could also create a client version under client folder, and a server version under server folder. You just create a method with the same name. This is only If you need different logic between server and client.

@cleverbeagle cleverbeagle changed the title import methods from client or use ValidateMethod Make sure to import methods into /imports/startup/client/index.js Sep 20, 2017
@cleverbeagle
Copy link
Owner

Flagging this as a refactor. Ideally, we should do what we do on the server. So,

  • List all methods inside of a /imports/startup/client/api.js file.
  • Pull that /api.js file into /imports/startup/client/index.js.

@tafelito can I get a spot check that I'm understanding this correctly?

@tafelito
Copy link
Author

Yes thats correct.

Another way of doing it is maybe just create an api.js shared between client/server and another api.js for each server and client for those cases where you need a client-only or server-only methods

something like

imports/startup/api.js (shared methods)
imports/startup/client/api.js (client only, not really used often)
imports/startup/server/api.js (server only)

then you pull both api.js into /imports/startup/<client/server>/index.js

I'm not sure if I like putting the api.js at the imports/startup level, but that's just an option.

The advantage of this is that you don't have to write the same imports in both client/api.js and server/api.js but the disadvantage is that you end up with more files and may bring more confusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants