javascript Ember.js - 如何从控制器触发视图方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15991871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ember.js - How to trigger view method from controller?
提问by Wojciech Bednarski
I'm trying to call view method from controller, but no idea how to do this. From view I can easily call controller method like this.get('controller').send('method');
我正在尝试从控制器调用视图方法,但不知道如何执行此操作。从视图中,我可以轻松调用控制器方法,例如this.get('controller').send('method');
How to do something like that from controller this.get('view').send('method');
?
如何从控制器做类似的事情this.get('view').send('method');
?
To give you better overview what I'm trying to do.
为了让您更好地了解我正在尝试做什么。
I have application controller Ember.Controller.extend({})
I have application view Ember.View.extend({})
and application template.
我有应用程序控制器Ember.Controller.extend({})
我有应用程序视图Ember.View.extend({})
和应用程序模板。
In application template is login form, when user submit it controller method is executed. In this method if login credentials are incorrect I need to call view method which is executing jQueryUI
method on login form (shake method to be exact and showing some text).
在应用程序模板中是登录表单,当用户提交时执行控制器方法。在此方法中,如果登录凭据不正确,我需要调用jQueryUI
在登录表单上执行方法的视图方法(确切地说是摇动方法并显示一些文本)。
回答by Christopher Swasey
This sounds like a good use for Ember.Evented
. By using event subscription and dispatching you can avoid coupling your view and controller.
这听起来很适合Ember.Evented
. 通过使用事件订阅和调度,您可以避免耦合视图和控制器。
Simply mixin Ember.Evented
:
简单地混合Ember.Evented
:
Controller = Ember.Controller.extend(Ember.Evented)
Now you can call on
and trigger
methods on your controller, to subscribe to an event and then to kick off the event. So, in your view you might do:
现在您可以在控制器上调用on
和trigger
方法,订阅一个事件,然后启动该事件。因此,在您看来,您可能会这样做:
didInsertElement: function () {
this.get('controller').on('loginDidFail', this, this.loginFail);
}
And then in your controller call this.trigger('loginDidFail')
to kick off your loginFail
view method.
然后在您的控制器调用this.trigger('loginDidFail')
中启动您的loginFail
视图方法。
Remember to remove the handler after the view is dismissed... see the answer below.
请记住在视图关闭后删除处理程序...请参阅下面的答案。
回答by Aaron Storck
Just wanted to answer on this question to address the issue with properly removing the listener if the view is cleared (when the route changes). It's also not necessary to use a jquery proxy, since the on/off methods support a target, which is good because unsubscribing a proxy is definitely more complicated. Revising what Christopher provided:
只是想回答这个问题,以解决在清除视图(路由更改时)时正确删除侦听器的问题。也没有必要使用 jquery 代理,因为 on/off 方法支持目标,这很好,因为取消订阅代理肯定更复杂。修改克里斯托弗提供的内容:
didInsertElement: function()
{
this.get('controller').on('loginDidFail', this, this.loginFail);
},
willClearRender: function()
{
this.get('controller').off('loginDidFail', this, this.loginFail);
}
Without removing the subscription any subsequent visits to the login route (without reloading the page) will add additional listeners; i.e. memory leaks, errors, and unexpected behavior.
在不删除订阅的情况下,对登录路由的任何后续访问(无需重新加载页面)都会添加额外的侦听器;即内存泄漏、错误和意外行为。