Skip to content

Instantly share code, notes, and snippets.

@caseywatts
Last active July 5, 2017 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caseywatts/6b0ecf13da4fea3949ff92551eb921e0 to your computer and use it in GitHub Desktop.
Save caseywatts/6b0ecf13da4fea3949ff92551eb921e0 to your computer and use it in GitHub Desktop.
ember-buffered-proxy usage

This example is for an ember-data model named 'plan'.

Proxy creation can be done in a computed property in the controller/component. It won't re-compute unless the entire plan changes (properties on plan can change and it won't trigger this). And then we can use applyBufferedChanges inside of the actions hash save action.

  // controller plans/edit.js
  planProxy: computed('plan', function() {
    return BufferedProxy.create({content: this.get('plan')});
  }),
  actions: {
    savePlan() {
      this.get('planProxy').applyBufferedChanges();
      this.get('plan').save();
    }
  }

And then to keep things clear, a component can get along just fine with no awareness that there's a proxy involved.

// route template for plans/edit.hbs
{{edit-plan
  plan=planProxy
  savePlan=(action 'savePlan')
}}
// component template plan-edit.hbs
<div>
  {{plan.name}}
</div>

<button {{action savePlan}}>
  Save Plan
</button>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment