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

查看:76
本文介绍了新的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')

  • 从视图中:视图不应访问路由器,但发送到控制器的事件将会起泡。例如: 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

**更新**

我在一月的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天全站免登陆