sails.js: Lifecycle callbacks for Models: Do they support beforeFind and afterFind?

╄→гoц情女王★ 提交于 2019-12-12 15:56:48

问题


In sails.js, Models support lifecycle callbacks for validate, create, update and destroy.

Is there support for callbacks for find() or query as well? Like beforeFind() and afterFind()?

The idea is same. I would want to validate / modify parameters before the query is run or after the query is run.

Any thoughts?


回答1:


As of writing this it does NOT support these requests, however their is a pull request https://github.com/balderdashy/waterline/pull/525

You can use policies to do this in the mean time.




回答2:


i don't get why this was left out in the first place. It's actually logical to want to add some data to the fetched model data after each model find.

The closest thing to afterFind in the documentation as of writting is customToJson model setting.

customToJSON: function() { 
    // Return a shallow copy of this record with the password and ssn removed. 
    return _.omit(this, ['password', 'ssn']) 
}

you basically do your stuff before the return omit part. I still don't get why these lifecycles were omitted.

I think I am going to write a hook to provide these for now. I will post it here.



来源:https://stackoverflow.com/questions/28167738/sails-js-lifecycle-callbacks-for-models-do-they-support-beforefind-and-afterfi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!