2

任何人都知道如何为一个视图部分加载模型,然后为另一个视图加载整个模型?

例如:

  /*
    This will get all projects, so we only want an id and name returned from the server.
    Each project is a monster, so we don't want all data for each project.
  */
  App.ProjectsRoute = Ember.Route.extend({
    model: function () {
      return App.Project.find();
    }
  });

  /*
    This is a project detail, so we'd want the entire model returned from the server. Embedded records and all.
  */
  App.ProjectRoute = Ember.Route.extend({

  });

我能找到的最接近的是:https ://stackoverflow.com/a/14183507/1125563

在那我可以做这样的事情:

  App.ProjectRoute = Ember.Route.extend({
    setupController: function(controller, model) {
      if (model.get('isTeaser')){
        model.reload();
      }
    }
  });

在这个解决方法中,我有一个计算属性 isTeaser,它检查一些东西以确定我是否只加载了它的一部分。

除了有点凌乱之外,这里唯一的交易破坏者是它正在过渡到带有部分加载模型的路线,然后在加载之后,所有的东西都会有点优雅地咬合。不是粉丝..

我错过了一些明显的东西吗?

4

1 回答 1

1

这是我消除初始渲染延迟的方法。这就是你所说的“不雅点”吗?

// Load the light version of all subjects on page load
App.ApplicationController = Em.Controller.extend({
  init: function() {
    return App.Subject.find();
  }
});

// Fetch all our previously loaded subjects
App.SubjectsRoute = Ember.Route.extend({
  model: function() {
    return App.Subject.all();
  }
});

App.SubjectRoute = Ember.Route.extend({
  // If we're loading the page directly to this route, do a normal find
  model: function(params) {
    return App.Subject.find(params.subject_id);
  },
  setupController: function(controller, model) {
    // Show what details we have for this subject (e.g. the title) immediately
    controller.set("model", model);

    // Load full details for the model and display them as soon as they arrive
    if (Em.isEmpty(model.get("text"))) {    
      App.Subject.find(model.get("id")).then(function(model) {
        return controller.set("model", model);
      });
    }
  }
});
于 2013-06-30T21:18:32.560 回答