使用路由时如何访问视图属性和操作处理程序?(How can I access view properties and action handlers when I use routing?)

看来,当我在Ember.js中使用新的路由功能时,无法从其模板访问视图上定义的属性和操作处理程序。 相反,只有控制器的属性可用,应用程序在路由器中查找操作处理程序。

这是一个问题的例子: http : //jsfiddle.net/InMatrix/wtUML/3/

如果单击我的个人资料--->照片--->测试,您将在控制台中收到此错误:

无法响应状态root.profile.photos中的事件testButton。

显然,App.PhotosView中定义的以下动作处理程序不可用于该事件。

App.PhotosView = Em.View.extend({ templateName: 'photos', testButton: function(){ alert('test Button'); } });

有没有办法让我的动作处理程序保存在相关的视图类中? 我错过了什么吗?

It seems that when I use the new routing feature in Ember.js the properties and action handlers defined on a view is not accessible from its template. Instead, only properties of the controller is available and the app looks for action handlers in the router.

Here is an example of the problem: http://jsfiddle.net/InMatrix/wtUML/3/

If you click My Profile ---> Photos ---> test, you'll get this error in the console:

could not respond to event testButton in state root.profile.photos.

Apparently the following action handler defined in App.PhotosView is not available to the event.

App.PhotosView = Em.View.extend({ templateName: 'photos', testButton: function(){ alert('test Button'); } });

Is there a way I can keep my action handlers in the relevant view class? Did I miss anything?

最满意答案

操作的默认上下文已更改为路由器。 如果您希望视图处理操作,您需要像下面这样指定它:

<button {{action testButton target="view"}}>test</button>

这是你的小提琴的工作版本: http : //jsfiddle.net/wtUML/4/

The default context of actions have been changed to the router. If you want the view to handle actions you need to specify it like this:

<button {{action testButton target="view"}}>test</button>

Here is a working version of your fiddle: http://jsfiddle.net/wtUML/4/

更多推荐