Skip to content

Operation hooks improvements #486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from

Conversation

fabien
Copy link
Contributor

@fabien fabien commented Mar 5, 2015

This is a combined PR - because of the expected conflicts/overlap of changes.

It implements the following improvements:

To maintain 'state' between before and after hooks, a hookState (object) property was introduced to context.

For consistency, all instance-level operations (prototype.updateAttributes and prototype.delete) will now have access to the current instance as part of the hook's context:

  • prototype.save + before save: the instance will be available as instance (unchanged behavior).
  • prototype.save + after save: the instance will be available as instance (unchanged behavior).
  • prototype.updateAttributes + before save: because the instance should not be manipulated directly, the instance will be available as currentInstance to distinguish between the instance that can be directly modified (it is omitted now). In other words, currentInstance provides the current 'state' of the instance, and it should be regarded as immutable. Any modifications should be applied to context.data, which incorporates the partial nature of the updateAttributes operation.
  • prototype.updateAttributes + after save: in after hooks, this difference is not important, and thus, the instance will be simply be available as instance.
  • prototype.remove/destroy/delete + before save: again, the difference is irrelevant, so the instance will be available as instance.
  • prototype.remove/destroy/delete + after save: the instance will be available as instance.

@fabien
Copy link
Contributor Author

fabien commented Mar 5, 2015

@bajtos @raymondfeng please review

@fabien
Copy link
Contributor Author

fabien commented Mar 5, 2015

This should supersede #485

@fabien
Copy link
Contributor Author

fabien commented Mar 5, 2015

The next step would be some 'canned' before hooks which will do the following:

Model.observe('before delete', fetchInstances); // OR:

Model.observe('before delete', fetchIds);

function fetchInstances(ctx, next) { // queryOnDelete: 'full'
  ctx.Model.find({ where: ctx.where }, function(err, instances) {
    if (err) return next(err);
    ctx.hookState.instances = instances;
    next();
  });
};

function fetchIds(ctx, next) { // queryOnDelete: 'id'
  ctx.Model.find({ where: ctx.where, fields: ['id'] }, function(err, instances) {
    if (err) return next(err);
    ctx.hookState.ids = _.pluck(instances, 'id');
    next();
  });
};

Perhaps we can even allow this syntax: Model.observe('before delete', 'presetHookFnName');

And: Model.registerObserver('presetHookFnName', function(ctx, next) { ... }).

This would be the basis for #474 (comment)

@fabien fabien mentioned this pull request Mar 5, 2015
@fabien
Copy link
Contributor Author

fabien commented Mar 5, 2015

I'll make a new PR: #488

@fabien fabien closed this Mar 5, 2015
@altsang altsang removed the #review label Mar 5, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants