coffee-box
coffee-box copied to clipboard
make controllers RESTful
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'
how do you think? @qiao
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?
do you mean override res.render
method in the middleware to test xhr? good idea.