【问题标题】:Who to call Custom Mixin Function on Built-in-Model Operational Hook in LoopBack?谁在 LoopBack 的内置模型操作挂钩上调用自定义 Mixin 函数?
【发布时间】:2016-01-18 14:28:19
【问题描述】:

我在 common/mixin/log.js 文件中有一个 Mixin 脚本

module.exports = function (Model, options) {
var app = Model.app;

Model.observe('after save', function (ctx, next) {
    // Log CREATE/UPDATE operations on every model
    next();
});
};

我的 server/model.config.json 文件是

{
    "_meta": {
    "sources": [
      "loopback/common/models",
      "loopback/server/models",
      "../common/models",
      "./models"
    ],
    "mixins": [
      "loopback/common/mixins",
      "loopback/server/mixins",
      "../common/mixins",
      "./mixins"
    ]
  }
}

我有模型 common/models/class.js 和相关的 json 文件是 common/model/class.json

{
    "name": "class",
    ...
    "mixins": {
        "Log": true
    },
    ...
}

当我尝试任何创建/更新操作时

Model.observe('after save', function (ctx, next)

工作正常,但我也想在用户模型(内置用户模型)创建/更新操作上调用此函数,但它不起作用,尽管我在 node_modules/loopback/common/models/user.json 中添加了以下行

"mixins": {
        "Log": true
    }

有谁知道我的实施中的问题在哪里?

【问题讨论】:

    标签: node.js loopbackjs


    【解决方案1】:

    经过一番折腾,我终于在 github 上得到了解决方案 https://github.com/strongloop/loopback/issues/1973#issuecomment-172760264

    解决方案

    在这方面,内置模型有点不同,因为它们不完全参与环回引导加载周期。请创建一个文件:server/boot/user.js 并尝试类似:

    module.exports = function(app) {                                                              
       var User = app.models.User;
       User.mixin('Log');
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-06
      • 1970-01-01
      • 2015-07-22
      • 2015-02-19
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多