将对象传递给sails.js中的views / layout.ejs

我很难尝试传递一个对象到我的项目的布局,以获得我需要在导航栏中显示的类别列表。 我一直在尝试几个解决scheme在这里和所有涉及使用策略和分配res.locals.myVar = someObj,问题是res.locals和也使用req.options.locals.myVar只有在控制器操作的视图而不是布局

到目前为止我得到了这个。

// getRoomList POLICY

Room.find().exec(function(err, rooms) { if (err) { return res.badRequest('Something went wrong.'); } if (rooms) { res.locals.roomlist = rooms; next(); } else { res.notFound(); } }); 

// config / policies

 '*': 'getRoomList' 

//在layout.ejs中

 <%= roomlist %> 

我认为可以使用策略将数据保存到res.locals ,但是在我的实现中,我将数据保存到请求本身,并将其发送到控制器中的视图(对我来说更清晰)

configuration/策略/ getCategories

 module.exports = function(req, res, next){ Categories.find().exec(function(err, models) { if (err) return res.badRequest("Error"); req.categories = models; next(); }); } 

API /控制器/ ABCController.js

 module.exports = { index : function(req, res, next){ res.view('page/index', { categories : req.categories }); } } 

configuration/ policies.js

 ABCController : { index: ['getCategories', 'getProfiles'] // add this policy to any page who needs nav bar categories } 

**configuration/ routes.js

 '/home/': { controller: "ABCController", action: "index" }