问题
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