How to use multiple models with a single route in EmberJS / Ember Data?

前端 未结 8 2044
一整个雨季
一整个雨季 2020-11-27 09:16

From reading the docs, it looks like you have to (or should) assign a model to a route like so:

App.PostRoute = Ember.Route.extend({
    model: function() {
         


        
8条回答
  •  忘掉有多难
    2020-11-27 09:43

    This might not be best practice and a naïve approach, but it shows conceptually how you would go about having on multiple models available on one central route:

    App.PostRoute = Ember.Route.extend({
      model: function() {
        var multimodel = Ember.Object.create(
          {
            posts: App.Post.find(),
            comments: App.Comments.find(),
            whatever: App.WhatEver.find()
          });
        return multiModel;
      },
      setupController: function(controller, model) {
        // now you have here model.posts, model.comments, etc.
        // as promises, so you can do stuff like
        controller.set('contentA', model.posts);
        controller.set('contentB', model.comments);
        // or ...
        this.controllerFor('whatEver').set('content', model.whatever);
      }
    });
    

    hope it helps

提交回复
热议问题