Ember.js - 如何从控制器触发视图方法? [英] Ember.js - How to trigger view method from controller?

查看:93
本文介绍了Ember.js - 如何从控制器触发视图方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从控制器调用视图方法,但不知道如何做到这一点。从视图我可以轻松地调用控制器方法,如 this.get('controller')。send('method');



如何从控制器执行类似的操作 this.get('view')。send('method'); p>

给你更好的概述我正在努力。



我有应用程序控制器 Ember.Controller.extend({})我有应用程序视图 Ember.View.extend({})和应用程序模板。



在应用程序模板中是登录表单,当用户提交时,执行控制器方法。在这种方法中,如果登录凭据不正确,我需要调用视图方法,该方法在登录表单上执行 jQueryUI 方法(握手方法并显示一些文本)。

解决方案

这听起来很好用于 Ember.Evented 。通过使用事件订阅和调度,您可以避免耦合您的视图和控制器。



只需混合 Ember.Evented

  Controller = Ember.Controller.extend(Ember.Evented)

现在您可以在和触发器中调用控制器,订阅一个事件,然后启动事件。所以在你看来你可能会这样做:

  didInsertElement:function(){
this.get ).on('loginDidFail',this,this.loginFail);
}

然后在您的控制器调用 this.trigger 'loginDidFail')启动您的 loginFail 查看方法。



记住删除视图后的处理程序被关闭...请参阅下面的答案。


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');

How to do something like that from controller 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.

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).

解决方案

This sounds like a good use for Ember.Evented. By using event subscription and dispatching you can avoid coupling your view and controller.

Simply mixin 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:

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.

Remember to remove the handler after the view is dismissed... see the answer below.

这篇关于Ember.js - 如何从控制器触发视图方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆