我在我的order.json中定义了这个范围,它与分支和客户以及其他属性有关系。
"name": "Order",
"properties":{...},
"relations": {...},
"acls": {...},
"scope": {
"include": [
{"relation": "branch", "scope": { "fields": "BranchName" } },
{"relation": "customer", "scope": { "fields": "CustomerName" } }
]
}
这在所有 GET 请求中都按预期运行良好,结果如下
[
{
"OrderDate": "2018-01-12T17:52:21.000Z",
"CustomerId": 39,
"BranchId": 5,
"CustomerRef": "Order by Phone",
...
"CreatedBy": 1,
"id": 1,
"branch": {
"BranchName": "aaaa",
"id": 5
},
"customer": {
"CustomerName": "xxxx",
"id": 39
}
}
]
我期待类似的结果,但是,成功 POST 请求后收到的响应数组不包括相关模型中的分支名称和客户名称信息。
我做的是否正确? 或者是否有任何其他方法可以在创建/更新操作后从相关模型中获取信息。我只是想避免在创建/更新后立即发出另一个 GET 请求。
您可以在保存后使用操作钩子。
Order.observe('after save', function(ctx, next) {
if (ctx.instance) {
ctx.instance.relatedmodel = someFunctionToGetRelatedModel();
}
next();
});
ctx.instance
内的任何内容都应包含在环回响应中。
您只需要弄清楚如何无缝提取您想要包含的相关模型详细信息。