0
votes

I'm using Django REST Framework, not Rails (which seems to have several magical gems to make everything work swiftly with Ember) and I've been having some difficulties trying to figure out how Ember expects responses. I'm using Ember CLI, thus I'm also using Ember data.

The documentation states only the typical GET usage, when I'm simply retrieving an object or an array of objects. Documentation states:

The JSON payload should be an object that contains the record inside a root property

And about conventions:

Attribute names in your JSON payload should be the camelCased versions of the attributes in your Ember.js models.

No problem with that.

1. But how should the API respond when there are errors?

Ok, so documentation also states you could use ajaxError to check jqXHR status for an error and then return a populated DS.Error for the record. However, how should I return different kind of errors. For example, let's say the user session is now invalid and because of that the server couldn't delete a record as requested.

2. How will Ember submit requests?

I'm quite new to REST in general. I think Ember simply use the appropriate verb for the action it wants: GET, POST, PUT, DELETE. I think it's quite clear it will send all the model's field to POST a new one, but how about DELETE? Will Ember send all the record or just the ID to delete an object?

1

1 Answers

1
votes

Generally you should be able to see the requests Ember makes by just opening your browser dev tools and seeing the network requests.

Ember data likes the api to respond with an errors hash, something like this:

{"errors":{"title":["can't be blank"]}}

Then as long as you define a function to handle the error case:

Ember.Controller.extend({
  actions: {
    deleteUser: function() {
      var user = this.model;
      function success() {
        // do something cool? 
      }

      function failure() {
        user.rollback();    
      }

      user.destroyRecord().then(success, failure);
    }
  }
});

then user.errors will be automatically populated and you can do an if user.errors in your template.