简体   繁体   English

Ember.js中哪些合适的钩子在路由中实现“加载”状态?

[英]What are the right hooks in Ember.js to implement a “loading” state in a route?

While a Position model loads, I want to show a loading/spinner message in the template. 加载位置模型时,我想在模板中显示加载/旋转消息。 I am not being able to achieve that. 我无法实现这一目标。

{{#if loading}}
  <div class="spinner"></div>
{{else}}
  <div>Here goes the position</div>
{{/if}}

Given the router 给定路由器

App.Router.map(function() {
  this.resource('article', { path: '/:id' }, function() {
    this.route('position', { path: '*position_id' });
  });
});

my approach in the ArticlePositionRoute is the following: 我在ArticlePositionRoute中的方法如下:

App.ArticlePositionRoute = Em.Route.extend({

  actions: {
    loading: function() {
      var controller = this.get('controller');
      if (controller) controller.set('loading', true);
      return false;
    }
  },

  setupController: function(controller, model) {
    controller.set('loading', false); // BEFORE _super
    this._super(controller, model);
    controller.set('loading', false); // AFTER _super
  },

  model: function(params) {
    return promiseThatTakesAWhile(); // slow fetch model
  },

  afterModel: function(model) {
    var self = this;
    return promiseThatTakesAWhile().then(function(result) {
      model.set('result', result);
      self.set('controller.loading', false); // THIS THROWS "Uncaught Error: Property set failed: object in path "controller" could not be found or was destroyed."
    });
  }

});

In afterModel , the controller is not available, throws Uncaught Error: Property set failed: object in path "controller" could not be found or was destroyed. afterModel ,控制器不可用,引发Uncaught Error: Property set failed: object in path "controller" could not be found or was destroyed.

In setupController , when the set goes before the _super call, it blows with Error: Assertion Failed: Cannot delegate set('loading', false) to the 'content' property of object proxy <App.ArticlePositionController:ember726>: its 'content' is undefined. setupController ,当set _super调用之前发出时,它因Error: Assertion Failed: Cannot delegate set('loading', false) to the 'content' property of object proxy <App.ArticlePositionController:ember726>: its 'content' is undefined. , if I put it after the _super call, the view already calls didInsertElement and errs because it can't find the right div in the template (basically, it's too late). 如果我把它放在 _super调用,查看已调用didInsertElement和犯错,因为它无法找到合适的div模板(基本上,这是为时已晚)。

What are the proper hooks to set/unset the 'loading' flag? 设置/取消“ loading”标志的合适钩子是什么? I think I have the set loading true in the right place, but I don't know where to put set loading false . 我想我应该在正确的位置set loading true ,但是我不知道将set loading false放在哪里。

When the Ember docs say actions: { loading: function(transition, originRoute) { // displayLoadingSpinner(); ... Ember文档说出actions: { loading: function(transition, originRoute) { // displayLoadingSpinner(); ... actions: { loading: function(transition, originRoute) { // displayLoadingSpinner(); ... , if that example in the docs was fully developed, what would it look like? actions: { loading: function(transition, originRoute) { // displayLoadingSpinner(); ... ,如果文档中的示例已完全开发,它将是什么样?

You should use the built in loading route. 您应该使用内置的加载路线。 You may think it needs to exist as a child route of the resource that is taking time to load, but that's incorrect, it needs to be a child route of the parent's resource (which will have already resolved). 您可能认为它需要作为花费时间来加载的资源的子路径存在,但这是不正确的,它必须是父资源的子路径(已经解决了)。 Or in your case the loading route needs to exist as ArticleLoadingRoute . 或者在您的情况下,加载路径需要作为ArticleLoadingRoute存在。 It will be shown any time any resource/route immediately under it is taking time to load. 任何时候紧接其下的任何资源/路由都会花费时间来显示。

Here's an example where I've forced two resources to take a while to load 这是一个示例,其中我强迫两个资源加载一段时间

http://emberjs.jsbin.com/OxIDiVU/465/edit http://emberjs.jsbin.com/OxIDiVU/465/edit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM