-
Notifications
You must be signed in to change notification settings - Fork 29
Before all and after all hooks #11
Comments
+1 |
+1 I wouldn't mind having something like this for having hooks that apply to all service methods. var service = Service.extend({
before: {
all: [first.common.hook, second.common.hook]
}
}); |
I like the array syntax. For the implementation, it would be nice if myService.before({
all: [requireAuth]
});
myService.before({
create: [adminOnly]
});
myService.before({
all: [removeHelperData]
}); would result in while the others would all have only two: |
@marshallswain: @daffl and I were discussing in the office and thinking that is basically the case. The before Does that make sense? Thoughts? |
If you define it as an object on your service. What @marshallswain is saying for using |
Although, personally I would prefer this syntax: myService.before([requireAuth]);
myService.before({
create: [adminOnly]
});
myService.before([removeHelperData]); But this would make it inconsistent with how it is defined on a service object. |
It sounds like we're all thinking along the same lines. I really like the simplicity of passing the array by itself for all, as is done in @daffl 's last post. Consider this syntax: myService.before({
all: [requireAuth]
create: [adminOnly]
}); Would we also want to support the above and register |
You're right. Doing that would make it less inconsistent. So here is the API (which shouldn't break the existing): Array and Object notation. Works for both, when adding a // As an object
var before = {
all: isAuthenticated, // also as an array [ isAuthenticated ]
create: [ isAdmin, hashPassword ]
}
// As an array will simply become { all: <array> }
var before = [ isAuthenticated, isAdmin ]
// As a single method will become { all: [ <single> ] }
var before = isAuthenticated;
var myService = {
find: function(params, callback),
before: {
all: isAuthenticated, // also as an array [ isAuthenticated ]
create: [ isAdmin, hashPassword ]
}
};
app.use('/myservice', myService);
app.service('myservice').before({
all: [ foo ],
create: [ bar ]
}); Will register a chain of |
That looks good. |
👍 way up! |
The hooks plugin should provide the ability to also take just a single hook function (instead of an object) which will be applied to all service methods:
Additionally it could be very helpful to add general hooks to the application which will be applied to every service (this could be easiliy implemented as a service mixin):
The text was updated successfully, but these errors were encountered: