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

Allow Model#save with patch: true to take different attrs #3236

Merged
merged 2 commits into from
Aug 4, 2014

Conversation

akre54
Copy link
Collaborator

@akre54 akre54 commented Jul 23, 2014

Fixes #3232

@caseywebdev
Copy link
Collaborator

I find life is generally easier when I keep the keys in attributes identical to the keys that the server sends/receives. That said, giving people the option to do what they already can with save (POST/PUT) + toJSON in the save (PATCH) case seems reasonable, though their end of the code may start to get unwieldy.

@akre54
Copy link
Collaborator Author

akre54 commented Jul 23, 2014

Totally agree. I personally wouldn't do this, but I think we shouldn't stand in the way if you want to do it in your code.

@jashkenas
Copy link
Owner

Agreed, and agreed. Thanks!

jashkenas added a commit that referenced this pull request Aug 4, 2014
Allow Model#save with patch: true to take different attrs
@jashkenas jashkenas merged commit f7cc792 into jashkenas:master Aug 4, 2014
@akre54 akre54 deleted the save-and-patch-diff-attrs branch August 4, 2014 19:41
@spectras
Copy link

spectras commented Dec 8, 2014

Hello,
I don't know if I should be posting here or opening another issue.

I would like to suggest another way to handle this case, because the solution implemented in this PR implies bad separation of concerns:

  • I think the model should be responsible for converting the representation to and from the server.
  • This PR forces the caller to supply the server-side representation.

It is also inconsistent, as sync() and save() must be supplied the server-side representation when using the PATCH verb but not when using the PUT verb.

A possibly better approach could be:

  • add an optional argument for toJSON, specifying which attributes to serialize (if argument is not set, serialize everything as usual).
  • in sync(), always use toJSON before sending data. When patch option is true, pass it _.keys(attrs) as a list of attributes to serialize.

This way, the save() and sync() would be consistent in their patch/non-patch behavior, and the knowledge of when and how to convert the representation into server-side representation would remain encapsulated in the model.

Please tell me if I should open another issue, rather than adding to this one ^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PATCH support + Transforming keys between server and client
4 participants