-
Notifications
You must be signed in to change notification settings - Fork 50
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
Add Before and After hooks to Member and Collection Actions #259
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to see this use case handled, but in a way that doesn't entangle with the exiting functionality so much. I consider memberAction
and collectionAction
to be a low level API that users may not necessarily want serialization involved with. I would be happy with a generic customization opportunity, and a serialize pre-made handler that plugs into it, to accomplish the same outcome you have in this PR.
import { memberAction, serializeAndPush } from 'ember-api-actions';
export default DS.Model.extend({
// /fruits/123/ripen
ripen: memberAction({ path: 'ripen' , after: serializeAndPush }),
});
Also, please reword your commit messages to follow the conventional commits spec. this would be a minor release ( |
@mike-north Thanks for your feedback. Those are great suggestions and I've implemented them. Please have a look. |
addon/utils/serialize-and-push.js
Outdated
let modelClass = this.store.modelFor(modelName); | ||
let serializer = this.store.serializerFor(modelName); | ||
let normalized = isArray(response.data) ? serializer.normalizeArrayResponse(this.store, modelClass, response) : | ||
serializer.normalizeSingleResponse(this.store, modelClass, response); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the ember-data documentation, it seems that some arguments are missing
https://www.emberjs.com/api/ember-data/3.5/classes/DS.JSONSerializer/methods/normalizeArrayResponse?anchor=normalizeArrayResponse
It's possible that some of these are optional, but you'll have to open up a ticket in ember-data to be sure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good suggestion. This changes work for my use-cases but I agree that it's JSON API specific and that there may be cases where those functions require additional inputs. I wonder if we can split this PR so that it just adds the before
and after
hooks, then we can iterate on the serializeAndPush
feature in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, let's do two PRs. Thanks for the revisions.
Will extract as a separate PR
@mike-north Ok I've removed the |
🎉 This PR is included in version 0.2.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
@mike-north Thanks for this great addon.
This PR does a few things that I've found useful in my apps:
Provide
before
andafter
hooks that can be used to do things like serializer the outgoing payload and push responses into the store.Make it possible to have an empty path. I have legit uses cases for wanting to use the exact URL created by Ember Data without a suffix.
In the tests I show an example of how to push response into the store. This relates to #194. In a subsequent PR we can look at making this util a part of the addon.