问题
I am new in sails.js. I have worked on opencart. In sails.js, How can we reuse functionality like dynamic categories (based on the tables data). So in this case there will be a menu controller and menu.ejs view. What i need is there will be a parent controller and a parent view like HomeController.js and HomeView.ejs . Inside that menu part will be reuse. same like header controller/view , footer controller/view . So if i route to '/home' then it will call HomeController.js. Then home will load headerController this will send data to headerView. Now headerController will call menuController and menuController pass data to menuView. Please help me on this. Thanks in advance.
回答1:
You cannot nest controller calls since the res.view() method that you would call in each one of them is terminal.
This method is terminal, meaning it is generally the last line of code your app should run for a given request
The controllers do not render a view, they send an http response to the client calling methods like res.view()
or res.json()
.
For the HeaderController you are talking about, I guess the content is static and you should just write the content in the layout.
About the MenuController, I understand a menu may change depending on the request, you are not necessarily building a single page application and you may not want to call it by AJAX or websocket once the page is loaded. To solve it in a manner similar to your description, you can use the possibility to render views independently from controllers.
You could create a service to perform it:
// api/service/partials.js
module.exports = {
/**
* Renders the menu and pass the result in a callback cb(err, html)
* The "req" argument is present like in a controller method, but not "res"
* "data" could be an optional argument to pass extra configuration
*/
menu: function(req, data, cb) {
// Write here the logic to build the menu depending on the request
// Access to the express app to render the "menu.ejs" template
// see http://expressjs.com/3x/api.html#app.render
sails.hooks.http.app.render('menu', data, cb);
}
};
Example of template to render:
<!-- views/menu.ejs -->
This is the menu <%= data %>
At this point, you could simply call partials.menu(req, data, function(err, html) {})
in all your controllers and inject the generated html in the controller's view. But that would be a lot of duplicate code. Let's write a policy to perform it:
// api/policies/dynamicMenu.js
module.exports = function(req, res, next) {
// Call the service that generate the content of the menu
// and inject the result in a "menu" variable accessible in the controller's template
partials.menu(req, {data: 'data test'}, function(err, html) {
res.locals.menu = html;
next();
});
};
You could write one policy per "partial" (ex: one for the menu, another one for the footer ...). Apply the policy to controllers:
// config/policies.js
module.exports.policies = {
// ...
// Apply the policy to controllers that display the menu
'aControllerThatDisplaysTheMenu': {
'*': ['dynamicMenu']
}
// ...
}
Finally, display the menu in your layout file:
<!-- views/layout.ejs -->
...
<%= menu %>
...
This is just a quick example, you have to handle errors and adapt it to better fit your needs.
回答2:
The principal in server side framework MVC like Sails, also same as Laravel (PHP) and the other, controller is not binding to partial element in the view/ front end component. At server side framework, controller more behave like bridge with logical behavior between view/ user interaction (can be done via REST API or complete rendered HTML view) to model which represent storage system and agnostic to any database/ cache system. So controller is more represented by routes
, not by partial view/ component.
If you want "controller" with behavior that attached to partial views/ component like that, it's front end framework's job, like Angular, Ember, etc.
来源:https://stackoverflow.com/questions/32212856/sails-js-send-data-from-partial-controller-to-partial-view