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

Added support for nested method definitions #22

Closed
wants to merge 1 commit into from

Conversation

mmacaulay
Copy link

No description provided.

@tedeh
Copy link
Owner

tedeh commented Apr 8, 2014

The build fails for some reason, so I can't accept this. Also, I would think that giving the server an option in the form of a function "router" that takes a JSON-RPC method string and returns the correct method is a better idea. Like this:

var jayson = require('jayson');
var methods = {
  add: function(a, b, done) { done(null, a + b); }
};
var server = jayson.server(methods, {
  router: function(method) {
    if(typeof(this[method]) === 'function') return this[method];
    if(method === 'add_2') return this.add.bind(null, 2);
    // nothing returned is a non-existing method error
  }
})

You might call add_2 on the server to return a function that adds 2 to a single number, for instance. It would be trivial to add the type of routing that is in your pull request with this solution, without breaking backwards compatibility.

@tedeh tedeh closed this Apr 8, 2014
@tedeh tedeh mentioned this pull request Apr 8, 2014
@mmacaulay
Copy link
Author

Hi,

I think the build failure is unrelated to this PR, I am getting the same failing tests locally on your master branch as here: https://travis-ci.org/tedeh/jayson/jobs/21887288

As for the router, I'm not sure that's really going to achieve the same thing. I'm hoping to bind more complex objects to the jayson server instead of a flat list of methods.

Maybe some code will help explain what I'm doing with it:

  return jayson.server({
    User: require('./rpc/user'),
    Login: require('./rpc/login'),
    etc.
  })

Where ./rpc/user looks something like this:

module.exports = {
  index: function() { /* get the users */ }
  show: function(id) { /* get a user */ }
  etc.
}

Not sure how I can accomplish that with the router without building a really big methods object where I'm defining methods like User_index and then mapping those onto the real domain objects anyway.

@tedeh
Copy link
Owner

tedeh commented Apr 10, 2014

If you look below the "Method Routing" part in the README, you will find an example of how you can add a nested object to the server by flattening it. You don't need the router to do that.

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

Successfully merging this pull request may close these issues.

2 participants