【问题标题】:overriding blueprint configuration per-controller in controller in Sails.js在 Sails.js 的控制器中覆盖每个控制器的蓝图配置
【发布时间】:2016-11-11 10:05:04
【问题描述】:

我是sails 框架的新手。我正在使用sails 0.12(在cmd v0.11.5 上)。

我只想覆盖特定控制器的蓝图操作。为此我在我的控制器中有如下覆盖配置:

_config:{ defaultLimit : 3 , rest:true,actions:true,shortcuts:true },

但它不起作用,它采用 blueprint.js 的默认配置。

那么,我怎样才能为每个控制器更改它们?

【问题讨论】:

    标签: javascript node.js object model-view-controller sails.js


    【解决方案1】:

    【讨论】:

    • 我已经推荐了他们,但没有从他们那里得到任何帮助。我写了同样的代码
    • 你错了。我已经要求每个控制器配置。无论你建议的是整个应用程序,都意味着所有控制器通用。
    • 我首先为所有控制器设置了restful route false,然后我覆盖了catscontroller restful route。如果我仍然理解错误,则需要更多描述。
    • 我做了同样的事情。但没有工作。只有 blueprints.js 设置占主导地位。
    【解决方案2】:

    为此,您需要从您的模型中创建一个同名的控制器。在那里,您可以启用/禁用特定模型的蓝图路由并覆盖端点。

    例如:

    // if you have an User model at api/models/User.js
    // the controller below must be at api/controllers/UserController.js
    
    /**
     * UserController
     *
     * @description :: Server-side actions for handling incoming requests.
     * @help        :: See https://sailsjs.com/docs/concepts/actions
     */
    
    module.exports = {
    
      _config: {
        rest: false
        // It's possible to disable some route, but you can't enable them if your
        // main configuration is set "true". (Sorry, I really don't know why of this)...
        // the main configuration file is located at: config/blueprints.js
      }
    
    };
    
    

    此外,它们没有“defaultLimit”配置。要覆盖端点,你只需要实现方法:

    // api/controller/UserController.js
    /**
     * UserController
     *
     * @description :: Server-side actions for handling incoming requests.
     * @help        :: See https://sailsjs.com/docs/concepts/actions
     */
    
    module.exports = {
    
      create: function (req, res) {
        console.log('custom create method called!');
        // res.ok(populatedRecord);
      },
    
      find: function (req, res) {
        console.log('custom find method called!');
      }
    };
    

    【讨论】:

      【解决方案3】:

      另一种覆盖蓝图端点的方法是创建一条到同一端点的新路由:

      // config/routes.js
      'POST /user': { action: 'create-user' },
      

      这通常会更好,因为您可以使用操作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-29
        • 1970-01-01
        • 1970-01-01
        • 2015-10-27
        • 2014-04-11
        相关资源
        最近更新 更多