Group meteor methods in namespace?

Hi

I was wondering if it would be an improvement if you could group your meteor server methods per namespace.
Now for example, my filestructure groups the functions per class or subject.
For example a coffee file for playlists or a coffee file for parents.
Then a function in this playlist file might be ‘addToPlayList’ or in the other file ‘addParent’

For sake of organising and to prevent long unnecessary function names, I would like to register a method like (coffeescript):

Meteor.methods
  playLists:
    add: ( params ) ->
      #some code here

    remove: ( id ) ->
      # some code here

and

Meteor.methods
  parents:
    add: ( params ) ->
      #some code here

    remove: ( id ) ->
      # some code here

Then calling the method would be something like this:

Meteor.call  'playLists.add', params, ( error, result ) ->
  # return handle code

I don’t think something like this is currently possible, but with a small change the Meteor.call function could check if the prosed method name is an object path, and if so, try to see if the objects.key is a function and execute.

This request is not something extremely important, but it would allow for more semantic and short method names that belong to a certain ‘class’.

Would love to hear opinions.

They currently aren’t nestable like that, but nothing is stopping you from defining them like so:

Meteor.methods
  'playList.add': ->
    #some code here
  
  'playList.remove': ->
    #other code here

Also you could easily write a method that would accomplish this.

Meteor.scopedMethods = (scopeName, methodMap) ->
  scopeMap = {}
  _.each methodMap, (method, name) ->
    scopeMap[scopeName + '.' + name] = method

  @methods scopeMap

Then just use it like:

Meteor.scopedMethods 'playList',
  add: ->
    #code here

  remove: ->
    #code here
5 Likes

Yes, you are absolutely right. Adding the dot notation in the string is of course an option.
And I like your other suggestion even more. Thanks for that.

I wasn’t really thinking out of the meteor box here :smile:

1 Like

When I tried to use dot notation, I got an error message in the console saying "Unexpected token . "

You have to wrap the method name in “quotes” to declare the key as a string

2 Likes