我有一个带有 json 类型负载的模型。
content.json
...
"properties": {
"payload": {
"type": "object",
"required": true
}
},
...
我想在调用 updateAttributes 时保护它的一部分不被覆盖。
Content.beforeRemote('prototype.updateAttributes', function (ctx, unused, next) {
if (ctx.instance && ctx.instance.contentTypeId === 'folder') {
// TODO: Do not allow direct modification of the folder items (should use exposed API).
// Strip writes to payload.items and payload.itemIds
}
next();
});
实现此目标的最佳方法是什么?
都不是
delete ctx.req.body.payload.items
也不
delete ctx.args.data.payload.items
也不
delete ctx.instance.payload.items
做我想做的。
我是否必须完全覆盖 updateAttributes 方法?
最佳答案
根据我的经验,没有其他方法可以在不覆盖 updateAttributes
的情况下解决您的问题。
我在 boot
文件夹中有一个名为 override-defaults.js
的文件,其中包含以下代码。
var overrideUpdateAttributes = function (Model, fields) {
var updateAttributes = Model.prototype.updateAttributes;
Model.prototype.updateAttributes = function (data, cb) {
data = _.omit(data, fields);
if (Object.keys(data).length) {
updateAttributes.call(this, data, cb);
}
else {
cb(utils.createError('Request body cannot be empty', 400));
}
};
};
module.exports = function (app) {
overrideUpdateAttributes(app.models.UserUser, ['id', 'created', 'lastLogin', 'deleted', 'deletedAt']);
};
关于javascript - 环回中的写保护属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32233520/