Skip to content
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

Merged
merged 14 commits into from
Nov 1, 2018

Conversation

iezer
Copy link
Contributor

@iezer iezer commented Oct 26, 2018

@mike-north Thanks for this great addon.

This PR does a few things that I've found useful in my apps:

  1. Provide before and after hooks that can be used to do things like serializer the outgoing payload and push responses into the store.

  2. 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.

Copy link
Owner

@mike-north mike-north left a 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 }),
});

@mike-north
Copy link
Owner

Also, please reword your commit messages to follow the conventional commits spec. this would be a minor release (feat: XYZ)

@iezer
Copy link
Contributor Author

iezer commented Oct 28, 2018

@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 Show resolved Hide resolved
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);
Copy link
Owner

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

Copy link
Contributor Author

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.

Copy link
Owner

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.

addon/utils/serialize-and-push.js Outdated Show resolved Hide resolved
addon/utils/serialize-and-push.js Outdated Show resolved Hide resolved
addon/utils/serialize-and-push.js Outdated Show resolved Hide resolved
@iezer iezer changed the title Options to Push Payload and Serialize Request Add Before and After hooks to Member and Collection Actions Oct 29, 2018
Will extract as a separate PR
@iezer
Copy link
Contributor Author

iezer commented Oct 29, 2018

@mike-north Ok I've removed the serializeAndPush exported util and moved it to #260

@mike-north mike-north merged commit 613c71a into mike-north:master Nov 1, 2018
@mike-north
Copy link
Owner

🎉 This PR is included in version 0.2.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

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.

2 participants