简体   繁体   English

如何将事件从内部组件冒泡到Emberjs中的应用程序控制器?

[英]How to bubble events from inner components to application controller in Emberjs?

I am learning Ember and writing a simple login app with Emberjs. 我正在学习Ember,并使用Emberjs编写了一个简单的登录应用程序。 I have a login-box component - which handles the UI and backend communication. 我有一个登录框组件-处理UI和后端通信。 Upon successful login, I want to store current user somewhere so that all controllers can access current user object in the app. 成功登录后,我想将当前用户存储在某个地方,以便所有控制器都可以访问应用程序中的当前用户对象。 How can I do that? 我怎样才能做到这一点? I am bubbling up events from LoginBoxComponent using this.sendAction('action'). 我正在使用this.sendAction('action')从LoginBoxComponent冒泡事件。 The event is bubbling up to LoginController correctly(Login Controller's template invokes the component) - However since loginController is not in parent path of all controllers(I assumed applicationController is parent of all controllers - correct me if I am wrong), I am letting the event bubble up to application controller by not handling it in LoginController. 该事件正在正确地冒泡到LoginController(Login Controller的模板调用该组件)-但是由于loginController不在所有控制器的父路径中(我假设applicationController是所有控制器的父级-如果我错了请更正我),我让通过不在LoginController中处理事件,事件气泡上升到应用程序控制器。 However rails complains with 但是Rails抱怨

Error: Nothing handled the action 'userLogin'. 错误:什么都没有处理“ userLogin”操作。 If you did handle the action, this error can be caused by returning true from an action handler in a controller, causing the action to bubble. 如果您确实处理了该动作,则可能是由于从控制器中的动作处理程序返回true导致此错误起泡而导致的。

templates/login.hbs 模板/ login.hbs

<div class="col-sm-offset-3 col-sm-6">
  {{login-box store=store action="userLogin"}}
</div>

my success handler in components/login-box.js 我的成功处理程序在components / login-box.js中

  onSuccess: function(post) {
    console.log(post);
    this.sendAction('action');
  },

code in controllers/application.js controllers / application.js中的代码

  actions: {
    userLogin: function() {
      this.set('currentUser', '123');
      this.transitionTo('index');
    }

However, If I handle this action in applicationRouter, it gets handled correctly - I dont understand why its skipping application controller. 但是,如果我在applicationRouter中处理此操作,它将得到正确处理-我不明白为什么它跳过了应用程序控制器。 I dont want to handle it in application router because I can't set a global currentUser object from there. 我不想在应用程序路由器中处理它,因为我不能从那里设置全局currentUser对象。

Greatly appreciate any help. 非常感谢您的帮助。 Thanks! 谢谢!

Actions sent from components first go to the template's controller. 从组件发送的操作首先进入模板的控制器。

If the controller does not implement a handler for that action, it will bubble to the template's route, and then up the route hierarchy. 如果控制器没有为该操作实现处理程序,它将冒泡到模板的路由,然后上升到路由层次结构。 For more information about this bubbling behavior, see Action Bubbling . 有关此冒泡行为的更多信息,请参见“动作冒泡”

So your action is bubbling: Login Controller => Login Route => Application Route. 因此,您的操作正在冒泡:登录控制器=>登录路由=>应用程序路由。

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

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