3

I've seen this question.

Until nothing is done for ember-data failures handling what can i write to patch it, so that i can have a message if my request doesn't have a 200OK response?

I'm looking at ember-data source code, for example, here's the createRecord function

createRecord: function(store, type, record) {
    var root = this.rootForType(type);
    var adapter = this;
    var data = {};

    data[root] = this.serialize(record, { includeId: true });

    return this.ajax(this.buildURL(root), "POST", {
      data: data
    }).then(function(json){
      Ember.run(adapter, 'didCreateRecord', store, type, record, json);
    }, function(xhr) {
      adapter.didError(store, type, record, xhr);
      throw xhr;
    });
  }

I expected something like:

success: //do something
error: //do something else

But there's nothing like this. It's only a then after the ajax request.

What should I do? rewrite completely all the methods i need?

1 Answer 1

4

As of this commit ember data now uses promises (specifically the rsvp implementation), which replaces the success: and error: callbacks for a .then() style:

promise.then(function(value) {
  // success
}, function(value) {
  // failure
});

This is very similar to the previous callback style, but follows the promises style.

The first function is called on success and the second function is called on a failure. Looking at the code you posted shows that didError() is called on ajax failure, which is implemented in the adapter.

didError() (source) calls store.recordWasInvalid() if the status was 422, otherwise it calls store.recordWasError() (source).

store.recordWasInvalid() and store.recordWasError() transition the record into the isError or isValid = false state and triggers the becameError() and becameInvalid() events on the record. You can check these states or subscribe to these events to show your error message.

Sign up to request clarification or add additional context in comments.

1 Comment

As a heads up, we will be changing how promises are integrated into Ember Data soon. They still will be present but the mechanism will change a bit. We'll make sure to provide a thorough explanation when this happens.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.