简体   繁体   English

Ember.js - 如何从控制器触发视图方法?

[英]Ember.js - How to trigger view method from controller?

I'm trying to call view method from controller, but no idea how to do this. 我试图从控制器调用view方法,但不知道如何做到这一点。 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');执行类似的操作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方法的view方法( jQueryUI抖动方法并显示一些文本)。

This sounds like a good use for Ember.Evented . 这对Ember.Evented听起来很有用。 By using event subscription and dispatching you can avoid coupling your view and controller. 通过使用事件订阅和调度,您可以避免耦合视图和控制器。

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. 现在,您可以on控制器上调用和trigger方法,订阅事件,然后启动事件。 So, in your view you might do: 因此,在您看来,您可能会:

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. 记得在视图被解除后删除处理程序...请参阅下面的答案。

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. 它也没有必要使用jquery代理,因为开/关方法支持目标,这很好,因为取消订阅代理肯定更复杂。 Revising what Christopher provided: 修改克里斯托弗提供的内容:

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; 在不删除订阅的情况下,任何后续访问登录路由(无需重新加载页面)都将添加其他侦听器; ie memory leaks, errors, and unexpected behavior. 即内存泄漏,错误和意外行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM