Closed idchlife closed 9 years ago
They behave like Controllers in Rails, whereby you modify Model data before deciding which view to render (template, API data).
@keithwhor hm... If they are like that, how would I define something like that in nodal?
class UserController {
indexAction() {}
profileAction() {}
friendsAction() {}
settingsAction() {}
}
Thank you for answer in advance :+1:
Use Nodal's built-in command-line generators. You want one controller per route. If users/:id
is a route to your API's user endpoint, type nodal g:controller users
in your terminal and app/router.js
will be modified to have router.route(/^\/users\/?/, UsersController)
in it.
The controller itself supports a get
, put
, post
and del
method, each handler for its associated HTTP request type. Check the Controllers section of the README for more details.
You're free to extend your controller with additional methods. For example:
class MyController extends Nodal.Controller {
// handles HTTP GET request
get(self, params, app) {
if (params.id) { // a specific id is provided
return self.show(self, params.id, app);
}
return self.index(self, params.query, app);
}
// your methods
show(self, id, app) {
// do your show stuff
}
index(self, queryParams, app) {
// do your index stuff
}
}
@keithwhor
Nodal is an opinionated API Server and Framework for quickly generating RESTful API services API Server RESTful API
I'm an idiot, I'm sorry :) I missed it and thought that nodal is providing possibilities of framework like laravel/symfony2/rails etc for using it's controllers just like typical mvc, having any action inside and returning views, html etc.
Thank you for your answer!
Just to be clear, it still can.
get(self, params, app) {
self.render(app.template('my_template.html'), {data: 'my_data_object'});
}
will return a standard html file if my_template.html
is in your templates directory.
Check app/controllers/index_controller.js
and app/templates/index.html
in a new project to see an example of how this works.
I saw that each controller have several methods that are realted to name of the http method, but not actions. So... To clarify, are they really controllers or much like views from django (python)? Because if they are like views, maybe there is possibility to rename them?