1

When I invoke

 App.store.createRecord(App.User,  { name: this.get("name") });
 App.store.commit();

how do I know if its successful and how to wait for the asyn message?

MilkyWayJoe
  • 9,082
  • 2
  • 38
  • 53
Robin Wieruch
  • 14,900
  • 10
  • 82
  • 107
  • 1
    Ember-Data currently doesn't provide Error Handling when it comes to HTTP Statuses, but it's on the roadmap. As of now, one approach is to extend the adapter to use jQuery Ajax `statusCode` and pass the callback for each status code. [**This question**](http://stackoverflow.com/questions/13349035/emberjs-handle-401-not-authorized) is similar to yours in a way but it's more on the error handling side. – MilkyWayJoe Nov 28 '12 at 18:20

1 Answers1

9

Very limited error handling was recently added to DS.RESTAdapter in ember-data master.

When creating or updating records (with bulk commit disabled) and a status code between 400 and 599 is returned, the following will happen:

  • A 422 Unprocessable Entity will transition the record to the "invalid" state and will add any errors returned from the server to the record's errors property.

    The adapter assumes the server will respond with JSON in the following format:

    {
      errors: {
        name: ["can't be blank"],
        password: ["must be at least 8 characters", "must contain a number"]
      {
    }
    

    (The error messages themselves could be arrays of strings or just strings. ember-data doesn't currently care which.)

    To detect this state:

    record.get('isValid') === false
    
  • All other status codes will transition the record to the "error" state.

    To detect this state, use:

    record.get('isError') === true
    

More cases may eventually be handled by ember-data out of the box, but for the moment if you need something specific, you'll have to extend DS.RESTAdapter, customizing its didError function to add it in yourself.

Ian Lesperance
  • 4,961
  • 1
  • 26
  • 28