javascript - 如何在不触发模型生命周期回调的情况下更新 SailsJS 中的记录

标签 javascript sails.js waterline

我有一个模型,我需要在将新记录更新或保存到表后重建记录。问题是在 fixTree 进行更新时触发 afterUpdate 回调。

module.exports = {
  identity: 'Role',
  attributes: {
    ..
  },
  afterCreate: function(rec, cb) {
    fixTree();
    cb();
  },
  afterUpdate: function(rec, cb) {
    fixTree();
    cb();
  },
}

function fixTree() {
  /* code here */
  Role.update(....); // <--- do not trigger afterUpdate
  /* and code here */
}

最佳答案

我最终使用了 Controller Action

create: function(req, res) {
    var data = {};
    data.name = req.body.name;
    data.parent = req.body.parent;
    Role.create(data, function(err, record) {
        Role.tree.fix(function() {
            res.ok(record);
        });
    });
},
update: function(req, res) {
    var data = {};
    data.name = req.body.name;
    data.parent = req.body.parent;
    Role.update(req.params.id, data, function(err, record) {
        Role.tree.fix(function() {
            res.ok(record);
        });
    });
},

关于javascript - 如何在不触发模型生命周期回调的情况下更新 SailsJS 中的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26427356/

相关文章:

javascript - 如何在插件配置期间访问 $(this) 元素以使用 jQuery 获取 data() 属性?

node.js - 在 sails v1.0 中禁用模型的自动填充

javascript - 无论指定值如何,单选按钮总是向 MySQL 数据库返回 false 值?

javascript - 将 sails.js 应用作为 Azure WebJob 运行

node.js - 使用 sails js 版本 1 提升后的动态数据库连接

javascript - 如何在导出中包含导入的模块?

javascript - 使用 jQuery 将一个 div 移动到另一个 div

javascript - Dropzone.js - 显示存储在 app_data 文件夹中的图像

node.js - macos 上用于 sails js 开发的 Docker 图像挂起

node.js - 在 SailsJS 中维护模型更新历史