2016-10-28 48 views
0

在发生验证之后,我想在将模型保存到数据库之前更新模型。Strongloop回送验证和请求生命周期

什么是回环请求生命周期中的正确点(呃哦,这开始让我想起.NET webforms!)来做到这一点?

Report.validatesPresenceOf('basicInfo'); 
Report.beforeRemote('create', addCreatorId); 

function addCreatorId(ctx, instance, next) { 
    // alter the model, validation has not occurred yet 
} 

Report.observe('before save', sendToThirdParty); 

function sendToThirdParty(ctx, instance, next) { 
    // send contents to third party, alter model with response 
    // validation has not occurred yet 
} 

Report.afterRemote('create', sendEmail); 

function sendEmail(ctx, record, next) { 
    // model has been saved to the database 
    // validation occurs before this point 
} 

理想我想默认的回送模型验证触发被称为addCreatorIdsendToThirdParty功能之前。我应该怎么做呢?

我可以在我的before save钩子中明确地呼叫model.isValid(),但似乎我应该能够重新排列这些以便自动发生。

回送Operation Hooks文档在验证发生时没有提及,也没有提到Remote Hooks文档。

回答

0

“我想要在模型保存到数据库之前更新一个模型,确认后发生。” 我有这方面的解决方案。在循环中使用'persist'钩子,这是在验证之后并将数据保存到数据库之前调用的。您可以使用其'ctx.data'参数插入或更改任何想要的数据。希望它有帮助,但有点晚! 链接:https://loopback.io/doc/en/lb3/Operation-hooks.html#persist