yapplabs / ember-buffered-proxy

An Ember Proxy the enables change buffering
MIT License
166 stars 31 forks source link

what gets referenced in the proxy? #9

Closed samselikoff closed 9 years ago

samselikoff commented 9 years ago

I created a proxy to an Ember Data model, and the ED model had ember-validations mixed in. I was hoping my proxy would have a .validate method, but it didn't. Is this expected? Is there a way to bring that along as well?

Thanks for this lib, has saved me many times!

stefanpenner commented 9 years ago

i would just add that method to your proxy

samselikoff commented 9 years ago

I ended up doing this:

var model = this.get('model');
var MyProxy = BufferedProxy.extend(EmberValidations.Mixin, {
  validations: model.get('validations')
});
var proxy = MyProxy.create({
  content: model
});

does that look right? That lets me call proxy.validate()

lukemelia commented 9 years ago

properties are proxied, not methods. You can subclass BufferedProxy to add methods that delegate to the same method on content.

krisselden commented 9 years ago

@samselikoff an alternative is just to push the data into another model that extends your model, say your model is Item and you'd make a DraftItem that extends it, you can store.push('draft-item', dataFromItem) and use that DraftItem, destroy it on cancel, and you can push the data back to item on a successful save of DraftItem. Just add a DraftAdapter that redirects it back to item API to save. I've had success in forking models this way, and it rolls back relationship changes too (in this example though, Item is the master side of the relationships).

samselikoff commented 9 years ago

@krisselden I actually really like this solution. It makes sense too, because it is a separate model in the UI, and this seems to be the most robust + flexible.

I've been making some {{draftable-input}} components that build the proxy. But perhaps they could just take in a DraftModel from the outside world. I'll have to experiment a bit - thanks for the idea!

amk221 commented 9 years ago

@samselikoff It's a nice pattern, I have:

task: computed(function() {
  return this.createRecord('task');
}),
form: validatorFor('task', {
  date: {
    presence: true
  }
}),
actions: {
    save: function() {
        ...
        form.validate().then(function() {
          form.applyChanges();
          task.save();
        })
    }
}

But I worry what will happen when ObjectProxy's are deprecated