epilogue icon indicating copy to clipboard operation
epilogue copied to clipboard

Add a Controller to a resource

Open marutypes opened this issue 10 years ago • 3 comments

After looking over the code and checking out #34, I couldn't find any support for this. I suppose I could manually require in the base controller class, instantiate it and then add it to the list... but that is less exciting.

Anyway, if you can even give me a hint how I'd add this feature I might do so and do a pull request.

I just want to be able to add custom endpoints to an existing resource so I can use the hooks and whatnot without having to basically remake that part of the library.

Also, if it's already in the library and I'm dumb, please tell me! :)

EDIT: Example story: Say I have users with the usual scaffolding and want to add a special route to the resource (lets say it's GET users/chickens). I am fine with rolling my own logic for it, but I want to be able to add the controller to the resource so that epilogue does the router.get(blabla) for me, and exposes hooks on it.

marutypes avatar Jan 24 '15 01:01 marutypes

@TheMallen yeah I've been exploring different options. In terms of just providing all of the auto-generated routes for deep associations, it's not that difficult. What is difficult with the current design is implementing hooks for all of those routes.

I would be interested in discussing my current thoughts with you if you'd like (mbroadst@freenode), or certainly any PR with code would be a good way to get a conversation started. I have time this weekend to dedicate just to this, so I'll be working on some ideas that hopefully get us moving in the right direction.

mbroadst avatar Jan 24 '15 03:01 mbroadst

I'm a bit swamped this weekend, might be able to take a look see if I can add something simple, but I was thinking something with an api of:

    var users = rest.resource({
      model: db.models.User,
      endpoints: ['/users', '/users/:id'],
    });  
    var userPosts = users.subResource({
        model: db.models.posts
        endpoint: ['/posts', '/posts/:id']
    });
  //now I can add userPosts.auth or whatever hooks

Or alternatively,

    // uses the controller class to let you create custom routes off a resource with hooks
    var users = rest.resource({
      model: db.models.User,
      endpoints: ['/users', '/users/:id'], // ends up as '/users/:id/posts'
    });  
    users.controller('posts', {
        callback: function(req, res, next) {
            //manually do some sequelize
        },
        method: 'GET',
        endpoint: ['/users/:id/posts'] 
    });
//now I can use users.auth.posts and whatnot

If you want to do the discussion thing I have all sorts of messengers, but I am usually on hangouts ([email protected])

marutypes avatar Jan 24 '15 19:01 marutypes

Yeah, you've definitely hit the root of the problem: we need a way to have sub resources. I'm currently slogging through generating all the proper endpoints for @sharathm 's usecase (deep associations). The next step was going to be to maintain an internal auto-generated resource cache, but given that you could use a similar feature it seems to make sense to make this public api for extensibility. Let's see what you come up with for your case and what my requirements look like at that point

mbroadst avatar Jan 24 '15 20:01 mbroadst