什么是区分移动设备的最简单方法; sails.js中的桌面版

时间:2014-02-10 20:46:45

标签: javascript node.js sails.js

在sails.js下为移动设备用户提供自定义布局的最佳方法是什么?

(除负载严重条件的.ejs文件外。)

1 个答案:

答案 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.

呈现的回复