2

I have an AfterSave function that updates something else based on the previous value of the object that was just saved. Simply knowing that the object has changed with object.existed() is not enough.

Is there any way to get the previous value of an object's property without implementing a costly workaround and increasing the number of API calls?

Is it even possible to get the previous value of an object in the AfterSave function?

When I call object.previous("value"), I get an undefined value.

lschlessinger
  • 1,934
  • 3
  • 25
  • 47
  • can you use beforeSave instead? – dandavis Oct 26 '14 at 01:17
  • I don't think so. I'm trying to update a counter field on another class based on the change in value of another class. So before I update another class, I think it makes more sense to wait for the other class to save. I think `beforeSave` is typically used to enforce a particular data format. – lschlessinger Oct 26 '14 at 01:36

2 Answers2

0

Don't know if this is helpful or if it would be considered too costly of a workaround, but you could add a column to the object that is being updated that stores the previous value of the original column. This would allow you to access the previous value in the AfterSave function.

dstefanis
  • 247
  • 1
  • 7
  • 2
    Funnily enough, this is exactly what I ended up doing. It's definitely a decent temporary solution, but I hope Parse makes the `object.previous("value")` from Backbone soon. – lschlessinger Oct 30 '14 at 03:27
0

Unfortunately I don't have reputation to comment on dstefanis' approach.

This approach is good when one endpoint is changing the value - and indeed I've used it before to this end.

There's a critical limitation in that if multiple endpoints are writing, they may still keep over-writing both the "current" and "old" fields. This means it can't be used to give any strict assurances - e.g. "this field will never be decremented".

A conflict-safe way is to do a parse query by objectId, and pick up the old value. You can then do your own conflict resolution. I have this tested and working :-)