新 Ember 路由器的访问实例 [英] Access instance of new Ember Router

查看:26
本文介绍了新 Ember 路由器的访问实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何访问新的 Ember 路由器实例?API 文档似乎是指旧路由器或不正确:http://emberjs.com/api/classes/Ember.Router.html

How does one access the instance of the new Ember router? The API docs seem to be refer to the old router or are incorrect: http://emberjs.com/api/classes/Ember.Router.html

推荐答案

RouterV2 不容易通过全局常量访问,这使得以错误"的方式做事变得更加困难.要记住的主要事情是您不应该通过全局变量访问路由器(或其他任何东西).这样做通常是一种不好的做法,会导致代码很难测试.不幸的是,使用旧路由器很容易执行App.router.transitionTo('whatever') 之类的操作 - 您可以在各处找到此类示例,但这不是一个好主意.

RouterV2 is not easily accessed via a global constant, making it more difficult to do things the 'wrong' way. The main thing to keep in mind is that you should not be accessing the router (or anything else) via a global variable. Doing so is a generally bad practice, leading to code that is very hard to test. Unfortunately with the old router it was pretty easy to do something like App.router.transitionTo('whatever') - you can find examples of that all over the place, but it's not a good idea.

ember 不使用全局引用,而是只在需要的地方向路由器注入本地引用.

Instead of working with a global reference, ember injects local references to the router in just those places where it is needed.

  • 来自模型:无法访问.模型不应与路由器通话
  • 来自控制器:router = this.get('target')
  • 从一个角度来看:View 不应该访问路由器,但它发送到控制器的事件会冒泡.例如:this.get('controller').send('search', term)
  • 从模板:使用 {{action}}{{#linkTo}} 助手发送事件(通过控制器)或转换到另一个 url.
  • From a model: Inaccessible. Models should not be talking to the router
  • From a controller: router = this.get('target')
  • From a view: View should not access the router, but events it sends to the controller will bubble up. For example: this.get('controller').send('search', term)
  • From a template: Use the {{action}} or {{#linkTo}} helpers to send events (via the controller) or transition to another url.

更多详细信息,请参阅本次提交的注释:https://github.com/emberjs/ember.js/commit/5becdc4467573f80a5c5dbb51d97c6b9239714a8"

For more detail, see the notes on this commit: https://github.com/emberjs/ember.js/commit/5becdc4467573f80a5c5dbb51d97c6b9239714a8

** 更新 **

我为 1 月 EmberNYC 聚会的新路由器做了一个闪电般的谈话,其中包含更多细节 - 幻灯片在这里:我如何学会停止担心并爱上路由器

I put together a lightning talk on with more detail on the new router for the January EmberNYC meetup - slides are here: How I learned to stop worrying and love the router

这篇关于新 Ember 路由器的访问实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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