在sails.js下为移动设备用户提供自定义布局的最佳方法是什么?
(除负载严重条件的.ejs文件外。)
答案 0 :(得分:0)
你可以这样做:
express-device
检测设备,通过npm install --save git://github.com/marionebl/express-device
检测设备
// config/express.js
var device = require('express-device');
module.exports.express = {
// All your other express settings...
middleware: {
custom: true // This will not be needed on the next version of Sails
},
customMiddleware: function (app) {
app.use(device(app).capture());
app.enableDeviceHelpers();
app.enableViewRouting();
}
}
这将使req.device
可用,将设备保留在type
中。 app.enableViewRouting()
根据设备类型选择自动路由。已知类型为phone, tablet, desktop, tv, bot
文档:https://www.npmjs.org/package/express-device
根据设备类型分隔布局
要为设备类型创建单独的布局,您可以在layout.ejs
中创建views/[type]/
。 express-device
将自动选择匹配设备的特殊布局,如果未找到特定布局,则呈现默认布局。
views/
├── view.ejs
├── layout.ejs
└── [type]/
└── layout.ejs
如果在此示例中为type = 'phone'
,则来自移动电话的所有请求都会导致res.view('view')
使用views/phone/layout.ejs
。任何其他设备都会获得使用views/layout.ejs.