2016-10-28 50 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