Skip to content

unable to save Object.fromJSON() because it's not dirty #161

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

Closed
tmp120210 opened this issue Jan 8, 2016 · 7 comments · Fixed by #1295
Closed

unable to save Object.fromJSON() because it's not dirty #161

tmp120210 opened this issue Jan 8, 2016 · 7 comments · Fixed by #1295

Comments

@tmp120210
Copy link

Hello!

I'm using Parse library within angular.js framework.
To bind parse object to Angular model, I'm using Parse.Object.toJSON() method.
Then, I need to update data on parse. To do this, I create new parse object using method Parse.Object.fromJSON({json}) and then call save() method.
I found that it sends to server only _updateAt field.
If I manually call "set" method on this object fields, parse starts working as expected.
I think, there is an issue with "dirty" fields.

@andrewimm
Copy link
Contributor

This is by design -- when an object is constructed from a fromJSON call, it is assumed to represent the "server state" of that object. As such, it has no changes.

If you want to take the entire state of an object and send it to the server, you should construct an object like so:

new Parse.Object(json), where json is your blob from the toJSON method. This is shorthand for constructing a Parse Object, and then calling .set() with a blob of attributes.

This can be dangerous though. You should only be setting / saving the fields that have changed on your client. This is why we implement save the way we do. Here's an example:

I have a Parse Object accessible from multiple devices (might be a web app and an iPhone app, might be multiple installations of the same app, does not matter).

  1. I load the object on both devices
  2. On the first device, I change the field "name" to "Andrew" and save my object.
  3. On the second device, I change the field "admin" to true, and save my object.

If I only save the changes, both of them will be recorded on the server. If I save the entire object at once, though, the second device is not aware of the changes made by the first device. It sends the full copy of the object, and overwrites the changes made by the first object.

Hope this makes sense!

@korhanozbek44
Copy link

Hey @andrewimm. I use ParsePlatform in Vue and used ParseServerSDK. But there is a big problem as I told in link.
Actually this issue very helpfully. Because the Vue Reactive System not tracking change. I already spend a lot of time for "How can I track change from Vue side". Only solution is converting Parse Object instance to Json Object. But there is another problem here.

I convert Parse Object to Json and change the some Json propery value and save. The Parse doesn't detect change. It save all value also didn't change value. So this is problem because same time, if anyone change value, this algorithm override value.

@binarytide
Copy link

exact same use case here(vue + parse)

@flovilmart
Copy link
Contributor

I’m not familiar with vue in particular, but I believe you should approach the problem differently.

You should probably use an immutable state based on the server value, and the toJSON(). Then for recording mutation, you should treat them separately, and track them on a separate object.

When you user wants to ‘save’ you apply all the mutations on the original object, save() and render your component again, based on the server response / object state

@korhanozbek44
Copy link

@flovilmart Did you mean with "recording mutation", manually record somewhere changes or make a loop and detect which one change ?

@flovilmart
Copy link
Contributor

@dreadful44 yeah, basically your application state is the mix between the original object from the server + the list of mutations applied to the object. you can probably store the list of local mutation in a hashmap

{`${className}_${objectId}`: mutations}

Then the mutations can be just key / value pairs or a list of operations that the user performed on the top of the object (like [{key: 'newValue'}, {otherKey: {$increment: 1}}]) etc.

This has many advantages like providing an undo / redo very easily (push / pop from the mutations array)

I know this is working around the problem but it's a better strategy than sending back the whole object every times ;)

@korhanozbek44
Copy link

Ok @flovilmart Thanks for your respond. I'll try.

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 a pull request may close this issue.

5 participants