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 に答える