Ember initializing route model with query

前端 未结 2 586
情书的邮戳
情书的邮戳 2021-01-20 12:39

I am trying to initialize a Route\'s model with a DS query, as follows

App.Router.map(function() {
    this.resource(\'post\', { path: \'/posts/:post_slug\'          


        
相关标签:
2条回答
  • 2021-01-20 12:44

    Solved this with Deferred pattern.

    App.PostsRoute = Ember.Route.extend({
        model: function(params) {
            var records = App.Post.find({ slug: params.post_slug });
            var promise = Ember.Deferred.create();
            records.addObserver('isLoaded', function() {
                promise.resolve(records.get('firstObject'));
            });
            return promise;
        }
    });
    
    0 讨论(0)
  • 2021-01-20 12:57

    That should do that trick:

    App.Router.map(function() {
      this.resource('posts');
      this.resource('post', { path: '/posts/:post_id' });
    });
    
    App.PostsRoute = Ember.Route.extend({
      model: function() {
        return App.Post.find();
      }
    });
    
    App.PostRoute = Ember.Route.extend({
      model: function(params) {
        return App.Post.find(params.post_id);
      }
    });
    
    0 讨论(0)
提交回复
热议问题