3

What is the best, or just a good, practice for loading and saving relational data in Polymer elements' published properties?

I've used https://github.com/PaulUithol/Backbone-relational to load and save relational data. It depends on Backbone. But now with Polymer's use of Object.observe(), I mostly don't need Backbone's complex model objects (at least I don't need their get() and set() methods), but I can't figure out how I can best get rid of Backbone's complex model objects and just use plain JavaScript objects AND load and save relational data to my data store.

Is there a Polymer-compatible library/web component out there which already implements this? Or a native way to do it?

Here are a couple ways I could do it myself without a library, but I expect I'm missing lots of edge cases.

  1. Load relational data:
// Load from server
model = store.get('model-id');
// model == {'id':'abc', 'name':'Parent', child_ids:['child-id1', 'child-id2']}
for (child_id in model.child_ids){
    model.children[child_id] = store.get(child_id);
}
// Use model in Polymer element's published property here
  1. Save relational data:
// Get model from Polymer published property here
model.child_ids = [];
for (child in model.children){
    model.child_ids.push(child.id);
}
delete model.children;
store.set(JSON.stringify(model));
// or just store.set(model);
timblack1
  • 137
  • 7

0 Answers0