1

Lets try this explanation again...

I'm new to polymer (and getting back into web dev after a relatively long absence), and I'm wondering what the recommended approach might be to more closely manage object state while employing 2 way databinding. I am currently consuming rest API (json) objects. My question is if polymer keeps a copy of the original object before initiating updates to the bound object's properties/attributes...so one might be able to easily undo the changes? While allowing 2 way databinding to work its magic is often desired, there are cases where I'd like to prevent / delay changes to the object / DOM until the user approves the changes (say via the paper-dialog component for instance). I suppose one could make a temporary copy of the object and bind fields to that version, and then only persist the changes back to the source object upon user approval. In any case, I'd be interested to hear thoughts and see an example or two of recommended approaches (especially if I am off-track with my ideas!)

sinjins
  • 447
  • 10
  • 22

1 Answers1

2

I suppose one could make a temporary copy of the object and bind fields to that version, and then only persist the changes back to the source object upon user approval

This.

Consider that view-models are essentially different from pure data-models (sometimes called business-data). Frequently, the differences are irrelevant and one can use them interchangeably. However, be aware of scenarios where the view-model is distinct (uncommitted user edits are a good example).

The notion of a field editor that requires approval from the user is purely UI/View oriented. Whatever data is managed in that modality is purely in the domain of the view, and fetches/commits to the business-data should be discrete.

Scott Miles
  • 11,025
  • 27
  • 32
  • Thanks Scott. However, does this not lead into the problem described here: http://stackoverflow.com/questions/728360/most-elegant-way-to-clone-a-javascript-object?page=1&tab=votes#tab-top . If so, it seems clunky to me. Perhaps there is a better way to clone an object using javascript that I'm unaware of? (without relying on frameworks/jquery etc.) – sinjins Sep 08 '14 at 21:46
  • Ended up using the JSON stringify approach: http://stackoverflow.com/questions/20662319/javascript-deep-copy-using-json – sinjins Sep 09 '14 at 16:43
  • Yikes, stringify approach is inefficient. In any case, those problems are wrt copying _arbitrary_ javascript objects. In the scenario described one generally knows things about the data by virtue of having a UI for it. It's hard to make suggestions without knowing more about your project, but maybe try to factor the problem to use smaller pieces of data, instead of wholesale object graphs. E.g. instead of duplicating an entire 'customer' object up front, the 'address' editor duplicates only the 'address' fields. – Scott Miles Sep 10 '14 at 16:39