Ember - 自动重定向到firstObject [英] Ember - Automatically redirect to firstObject

查看:101
本文介绍了Ember - 自动重定向到firstObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个Ember路径 / clinic / 1 自动重定向以显示第一位医生: / clinic / 1 / doctor / 1

I'd like an Ember path /clinic/1 to automatically redirect to show the first doctor: /clinic/1/doctor/1.

每个诊所都有很多医生。

Each clinic has many doctors.

不幸的是,如果我使用这段代码:

Unfortunately if I use this code:

var doctor = clinicController.get('content.doctors.firstObject');
router.transitionTo('clinic.doctor.index', doctor);

...它不起作用,因为 content.doctors.length 仍然是 0 当此代码运行在 app_router.js

... it does not work, as content.doctors.length is still 0 when this code runs in app_router.js.

任何想法?

推荐答案

您应该可以这样做:

App.DoctorsRoute = Ember.Route.extend({
  model: function() {
    return App.Doctor.find();
  },

  redirect: function() {
    var doctor = this.modelFor('doctors').get('firstObject');
    this.transitionToRoute('doctor', doctor);
  }
});

这将工作,因为:


  • 如果模型钩子返回尚未加载的对象,则其余钩子将不会运行,直到模型完全加载。

  • 如果 redirect 钩子转换到另一个路由,其余的钩子将不会运行。

  • If the model hook returns an object that hasn't loaded yet, the rest of the hooks won't run until the model is fully loaded.
  • If the redirect hook transitions to another route, the rest of the hooks won't run.

请注意,从 2426cb9 起,您可以将隐含的 .index 转换时。

Note that as of 2426cb9, you can leave off the implicit .index when transitioning.

这篇关于Ember - 自动重定向到firstObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆