【问题标题】:Migrating to Ember.Router without Breaking Existing View-based {{action}} Handlers在不破坏现有的基于视图的 {{action}} 处理程序的情况下迁移到 Ember.Router
【发布时间】:2012-08-30 18:41:09
【问题描述】:

我有一个针对 emberjs pre-1.0 编写的代码库,没有使用 Ember.Router 或 Ember.StateManager。我正在尝试迁移到 Ember.Router,如果可能的话,迭代。

我创建了一个骨架 App.ApplicationController、App.ApplicationView 和 App.Router(只有一个路由)。 App.ApplicationView 只是包装了我现有的入口点视图:App.PreRouterMainView。一切正常,除了现有视图上的 {{action}} 绑定不再起作用

我经常使用{{action "someTargetMethod"}} 来调用PreRouterChildView.someTargetMethod(),但是在嵌入/'路由'PreRouterMainView 之后,现在在路由器上调用了someTargetMethod()。

有没有一种简单的方法让 PreRouterMainView 和子视图继续“以旧方式”工作,事件调用最近的视图而不是路由器上的方法?

或者我是否需要在一次重大更改中将所有视图方法重构为路由器方法?如果可能的话,我真的宁愿迭代地这样做。

【问题讨论】:

  • 试试{{action "someTargetMethod" target="view"}}

标签: ember.js


【解决方案1】:

您将不得不重构您的操作以明确定位视图(或有效路径上的任何其他对象):

{{action someTargetMethod target="view"}}

但是,动作的默认上下文已更改为路由器,因为路由器应处理大多数动作,尤其是更改状态/路由的动作。您可以调用控制器上的方法并在处理操作的路由器方法中查看(通过事件上下文)。如果动作没有改变状态或数据,那么有视图句柄就可以了。

编辑:内联代码文档表明您可以通过在控制器上设置属性来更改默认操作目标:

### Specifying a Target
There are several possible target objects for `{{action}}` helpers:

In a typical `Ember.Router`-backed Application where views are managed
through use of the `{{outlet}}` helper, actions will be forwarded to the
current state of the Applications's Router. See Ember.Router 'Responding
to User-initiated Events' for more information.

If you manaully set the `target` property on the controller of a template's
`Ember.View` instance, the specifed `controller.target` will become the target
for any actions. Likely custom values for a controller's `target` are the
controller itself or a StateManager other than the Application's Router.

If the templates's view lacks a controller property the view itself is the target.

【讨论】:

    猜你喜欢
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    • 2023-02-08
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多