[英]what is the easiest way of differentating mobile & desktop version in sails.js
What's the best way to have a custom layout for users of mobile devices, under sails.js? 在sails.js下为移动设备的用户提供自定义布局的最佳方法是什么?
(other than loads of heavily conditioned .ejs files.) (除了条件严重的.ejs文件的负载。)
You could do it like this: 您可以这样做:
express-device
, install via npm install --save git://github.com/marionebl/express-device
通过express-device
检测设备,通过npm install --save git://github.com/marionebl/express-device
Detect devices 检测设备
// 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();
}
}
This will make req.device
available, holding the device in type
. 这将使req.device
可用,将设备保持为type
。 app.enableViewRouting()
opts into automatic routing based on the device type. app.enableViewRouting()
根据设备类型选择自动路由。 Known types are phone, tablet, desktop, tv, bot
Documentation: https://www.npmjs.org/package/express-device 已知类型为phone, tablet, desktop, tv, bot
文档: https : //www.npmjs.org/package/express-device
Separate layouts based on device type 根据设备类型分开布局
To create a separate layout for a device type you can create a layout.ejs
in views/[type]/
. 要为设备类型创建单独的布局,可以在views/[type]/
创建layout.ejs
。 express-device
will automatically pick up the special layouts for matching devices and render the default layout if no specific layout is found. express-device
会自动为匹配的设备选择特殊的布局,如果找不到特定的布局,则会呈现默认布局。
views/
├── view.ejs
├── layout.ejs
└── [type]/
└── layout.ejs
If type = 'phone'
in this example, all requests from mobile phones will cause res.view('view')
to use views/phone/layout.ejs
. 如果在此示例中, type = 'phone'
,则来自手机的所有请求都将导致res.view('view')
使用views/phone/layout.ejs
。 Any other device will get a response rendered with views/layout.ejs.
任何其他设备都将使用views/layout.ejs.
呈现响应views/layout.ejs.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.