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

查看:17
本文介绍了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);

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

... 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天全站免登陆