【问题标题】:Sails.js - how to bind Custom blueprint action for all the models?Sails.js - 如何为所有模型绑定自定义蓝图操作?
【发布时间】:2015-10-08 11:40:49
【问题描述】:

我在文件 api/blueprints/count.js 中创建了自定义蓝图操作计数。

我想将此操作推广到所有模型。问题是当我添加这样的自定义蓝图操作时

         'get /:model/count': {blueprint: 'count'}

我在提升应用程序时收到此错误:

           error: count :: Ignoring attempt to bind route (/:model/count) to blueprint action (`count`), but no valid model was specified and we couldn't guess one based on the path.

我通过在配置属性中指定模型名称来解决问题

        'get /:model/count': {blueprint: 'count', model: 'user'}

或在地址中指定

         'get /user/count': {blueprint: 'count'}

这样指定的问题是我还需要为每个其他模型添加路由。有什么方法可以将这条路线概括为类似 'get /:model/count': {blueprint: 'count'}。

如果我们有这个功能,那就太好了。

请帮忙。

【问题讨论】:

    标签: node.js sails.js


    【解决方案1】:

    我们为此目的实现了自定义挂钩。您可以将其添加到您的api/hooks 文件夹中。

    /**
     * Adds support for count blueprint and binds :model/count route for each RESTful model.
     */
    
    import _ from 'lodash';
    import actionUtil from 'sails/lib/hooks/blueprints/actionUtil';
    import pluralize from 'pluralize';
    
    const defaultCountBlueprint = (req, res) => {
      let Model = actionUtil.parseModel(req);
      let countQuery = Model.count();
    
      countQuery.then(count => res.ok({count}));
    };
    
    export default function (sails) {
      return {
        initialize: cb => {
          let config = sails.config.blueprints;
          let countFn = _.get(sails.middleware, 'blueprints.count') || defaultCountBlueprint;
    
          sails.on('router:before', () => {
            _.forEach(sails.models, model => {
              let controller = sails.middleware.controllers[model.identity];
    
              if (!controller) return;
    
              let baseRoute = [config.prefix, model.identity].join('/');
    
              if (config.pluralize && _.get(controller, '_config.pluralize', true)) {
                baseRoute = pluralize(baseRoute);
              }
    
              let route = baseRoute + '/count';
    
              sails.router.bind(route, countFn, null, {controller: model.identity});
            });
    
          });
    
          cb();
        }
      }
    };
    

    【讨论】:

    • 谢谢。正如您在提升应用程序时所说的那样,我将它放在 api/hooks 中,它给出了以下错误: import _ from 'lodash'; ^^^^^^ SyntaxError: Unexpected reserved word
    • @ArunFebi 它是用 ES6 语法编写的。如果您没有使用最新的 NodeJS 版本或 Babel,那么只需将其重写为旧的 ES5 语法。
    • 当我解除应用程序时,我将代码 ES6 转换为 ES5。错误:count :: Ignoring 尝试将路由 (:model/count) 绑定到蓝图操作 (count),但没有有效模型已指定,我们无法根据路径猜出一个。
    • @ArunFebi 让我们进入聊天室,以便我们为您提供帮助。
    • @ArunFebi 还在挂钩中提供您当前的代码
    【解决方案2】:

    基于@ghaiklor 答案/代码,我创建了sails 可安装挂钩"sails-hook-blueprint-count" 以启用计数蓝图API 方法。

    “sails-hook-blueprint-count” 包可通过 npm 存储库 (https://www.npmjs.com/package/sails-hook-blueprint-count) 获得,您可以使用

    npm install sails-hook-blueprint-count
    

    命令。

    然后,当您升起帆应用程序时,您可以使用类似的路线

    GET /:model/count
    

    GET /:model/count?where={:criteria}
    

    :criteria 与 find where 蓝图方法 (http://sailsjs.org/documentation/reference/blueprint-api/find-where) 相同。

    响应将是带有格式的 json

    { 计数 : COUNT }

    【讨论】:

      猜你喜欢
      • 2015-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-08
      相关资源
      最近更新 更多