How to handle 404 of Ember Data in route?

后端 未结 3 1529
借酒劲吻你
借酒劲吻你 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: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.

提交回复
热议问题