coffee-box icon indicating copy to clipboard operation
coffee-box copied to clipboard

make controllers RESTful

Open layerssss opened this issue 11 years ago • 3 comments

Hi, some of controllers are designated to be used with xhr, but it will be a barrier for implementing third-party themes. So I propose to use a convention to make them work under both XHR and browser POST request:

  • each destroy and update method of contollers should invoke res.redirect 'back' if done
  • error handling should be unified and act like
if req.xhr
  res.statusCode = 400
  res.write err.message
  res.end()
else
  res.flash 'error',err.message
  res.redirect 'back'

layerssss avatar Dec 07 '12 03:12 layerssss

how do you think? @qiao

layerssss avatar Dec 07 '12 04:12 layerssss

Sorry for the late reply.

I'm thinking about separating the xhr operations into an ApiController, which can be registered as a middleware. This can keep the rest controllers clean(free of the xhr tests) but will also lead to duplicated codes in the ApiController.

Which is more acceptable?

qiao avatar Dec 07 '12 15:12 qiao

do you mean override res.render method in the middleware to test xhr? good idea.

layerssss avatar Dec 08 '12 07:12 layerssss