toranb / ember-data-django-rest-adapter

An ember-data adapter for django web applications powered by the django-rest-framework
MIT License
154 stars 27 forks source link

Set errors on model #88

Closed chripede closed 10 years ago

chripede commented 10 years ago

This makes it possible to handle server side errors on the client side.

Function was copied from the ActiveModelAdapter (https://github.com/emberjs/data/blob/v1.0.0-beta.8/packages/activemodel-adapter/lib/system/active_model_adapter.js#L102) and modified to work with DRF.

toranb commented 10 years ago

It seems like this actually broke the build - could you give that test a look and see if you can modify this? also -could you provide a true test case to show this in action? thanks !

chripede commented 10 years ago

@toranb I fixed the test. As you can see from the new speaker template it's now possible to display the exact field that causes the PUT request to fail.

dustinfarris commented 10 years ago

Is it possible to bind the errors to their respective model attributes?

dustinfarris commented 10 years ago

e.g.:

<form>
  <!-- non-field errors -->
  <div>{{#each errors}}{{this}}{{/each}}</div>

  <!-- field errors -->
  <div>{{#each location.errors}}{{this}}{{/each}}</div>
  {{input value=location}}
</form>
chripede commented 10 years ago

@dustinfarris It might be possible, but I don't think it should be done that way.

  1. It doesn't seem to be the ember way. Most of the code was copied from embers ActiveModelAdapter
  2. You would have to loop all your model fields in case you wanted to display all errors in a box
dustinfarris commented 10 years ago

Hmm, I'm not sure about the "ember" way. That adapter looks like it was specifically written for Ruby on Rails. As for the second concern, could we have a raw output property or something?

chripede commented 10 years ago

It's using DS.InvalidError from Ember-data. http://emberjs.com/api/data/classes/DS.InvalidError.html The DS.InvalidError must be constructed with a single object whose keys are the invalid model properties, and whose values are the corresponding error messages.

If you want the errors on each model property I think that's something you should do yourself in the error callback from the save function.

dustinfarris commented 10 years ago

Good point. It is good the way it is then.