How to handle 404 of Ember Data in route?

后端 未结 3 1541
借酒劲吻你
借酒劲吻你 2021-02-15 16:42

In my route I have a method that tries to request a list of models from the server

 model: ->
    App.MyModel.find
      projectId: (@modelFor \"project\").id         


        
相关标签:
3条回答
  • 2021-02-15 17:13

    Incidentally, the "new" BasicAdapter was just released now. The question for me was, will this make things easier to handle 404 errors.

    Approach #1

    My first approach - similar to what Christopher was suggesting - was to add an additional field containing the HTTP status.

      status: DS.attr("number");
    

    And then I used this AJAX call:

    $.getJSON(url, data).then(null, function(xhr) {
      return {
        id: id,
        statusCode: xhr.status
      };
    }).always(function(data) {
      return process(data).load();
    });
    

    What this does is to transform the error response (xhr) to a hash containing the requested id and the status code. Finally, the successful result or the failed hash are passed to the store.

    This kind of works, but isn't very practical: When you show a list of all model instances those "mock" instances have to be filtered out manually.


    Approach #2

    Another idea was to create a special error model.

    App.Error = App.Model.extend({
      status: DS.attr("number")
    });
    

    And the according query:

    $.getJSON(url, data).then(null, function(xhr) {
      return App.store.load(App.Error, {}, {
        id: 0,
        status: xhr.status
      });
    }).done(function(data) {
      return process(data).load();
    });
    

    This will load and create a new instance of the error model and put it into the store.

    The problem with this is that Ember wasn't really "accepting" this. The application just stopped routing, doing nothing anymore. So this seems like a dead end as well :(

    0 讨论(0)
  • 2021-02-15 17:23

    Bad news: right now, ember-data doesn't do anything when it gets a 404 on find(). At all. The model sits in the 'loading' state forever.

    There are no non-completely-stupid options, here, in my opinion. What I would probably do as a last resort is add a notFound attribute on my DS.Model, and instead of returning 404, return JSON with notFound set to true. It's painful, I know...

    --- I had originally offered a solution of overriding find in a subclass of RESTAdapter. Then I noticed that find DOES NOT get passed the record instance it is supposedly loading. So, no go on handling 404s by putting the record into an error state.

    [NOTE: ember-data has changed dramatically since March 2013, the information in this answer may no longer be operative]

    0 讨论(0)
  • 2021-02-15 17:29

    I hit this issue as well today.

    However, after looking at the source, it appears the model is actually setup to utilize Ember.Evented, and we can add our own handlers for these cases.

    The two events that caught my eye were becameError and didLoad.

    In my case I was able to do something like the following:

    // Grab a user by id.
    var user_rec = App.User.find( user.id );
    
    // Oh no! Error!
    user_rec.on('becameError', function() {
      alert('I could not find you!');
    });
    
    // We should be good! Proceed!
    user_rec.on('didLoad', function() {
      alert('Your email: '+this.get('email'));
    });
    

    Here's the source on github: https://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/model/model.js

    Hopefully, if this is indeed the way we should be handling things, there will be some added documentation in the guides in the near future.

    0 讨论(0)
提交回复
热议问题