InternalFX
InternalFX

Reputation: 1485

Is there a better way to avoid duplicate lifecycle callbacks?

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

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.

Upvotes: 1

Views: 502

Answers (1)

sgress454
sgress454

Reputation: 24948

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!

Upvotes: 2

Related Questions