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

Save with wait:true, patch: true and custom serialize() method #64

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Save with wait:true, patch: true and custom serialize() method #64

wants to merge 5 commits into from

Conversation

bobholt
Copy link

@bobholt bobholt commented Nov 13, 2015

fixes #52

When calling the save() method with wait: true and patch: true, the
current implmentation assumes a basic data structure and ignored the
structure generated by an overridden serialize method.

This fix invokes the serialize() method to ensure the user's data
structure is respected. In order for this to work with PATCH, we have to
inspect that structure to find where the attributes have been placed, and
then limit the properties of those object to only the ones changed.

This still makes some assumptions about the data structure (namely that all
attributes appear within a single object within the serialized structure),
but hopefully less restrictive ones than the current implementation.

When calling the `save()` method with `wait: true` and `patch: true`, the
current implmentation assumes a basic data structure and ignored the
structure generated by an overridden `serialize` method.

This fix invokes the `serialize()` method to ensure the user's data
structure is respected. In order for this to work with `PATCH`, we have to
inspect that structure to find where the attributes have been placed, and
then limit the properties of those object to only the ones changed.

This still makes some assumptions about the data structure (namely that all
attributes appear within a single object within the serialized structure),
but hopefully less restrictive ones than the current implementation.
@@ -20,6 +24,30 @@ var wrapError = function (model, options) {
};
};

// recursive function to find the attrs in the serialized object
function transformForPatch(obj, attrs) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change this to a var function like the others?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It still needs to be named because it's recursive. Is

var transformForPatch = function transformForPatch(obj, attrs) {

okay? Some folks think it's weird having both.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this is what I meant, thanks. I should have been more explicit, sorry for the confusion.

var clonedModel = new this.constructor(this.getAttributes({props: true}));
clonedModel.set(attrs);

options.attrs = assign(model.serialize(), attrs);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed commit here. This should be options.attrs = clonedModel.serialize();. Will add after fix above.

@bobholt
Copy link
Author

bobholt commented Nov 13, 2015

Thanks for the feedback @wraithgar. Additional commits to address those issues.

// if we're waiting we haven't actually set our attributes yet so
// we need to do make sure we send right data
if (options.wait && method !== 'patch') options.attrs = assign(model.serialize(), attrs);
if (options.wait && method !== 'patch') {
var clonedModel = new this.constructor(this.getAttributes({props: true}));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this the one that was causing errors when using this.serialize() then?

@wraithgar
Copy link
Contributor

I think I've asked all the questions I had when looking at this PR. Would love more eyeballs on this.

@bobholt
Copy link
Author

bobholt commented Nov 30, 2015

I've found some issues with this in production. I should be close to resolving, then will update this PR.

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.

When wait: true, serialization not applied to attributes passed to the save method.
2 participants