문제

Waterline allows for some lifecycle callbacks on models as listed below...

  • beforeValidation / fn(values, cb)
  • beforeCreate / fn(values, cb)
  • afterCreate / fn(newlyInsertedRecord, cb)
  • beforeValidation / fn(valuesToUpdate, cb)
  • beforeUpdate / fn(valuesToUpdate, cb)
  • afterUpdate / fn(updatedRecord, cb)
  • beforeDestroy / fn(criteria, cb)
  • afterDestroy / fn(cb)

However, what if I want to take action before create and update?

Rails had a beforeSave which was great for this. Is there something similar in sails.js?

I could have both callbacks call a function, but I want to be sure there isn't a better way.

도움이 되었습니까?

해결책

You can call beforeUpdate from beforeCreate, or vice versa:

beforeCreate: function(values, cb) {
    // Forward to the model's beforeUpdate method
    return User.beforeUpdate(values, cb);
},

beforeUpdate: function(valuesToUpdate, cb) {
    ...
}

Just keep in mind that the values sent via an update call will likely be different than the ones sent to create; for instance, don't rely on an instance's id value being available!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top