简体   繁体   English

如何将事件从Ember.js视图发送到其控制器

[英]How to send an event from Ember.js view to its controller

I have a custom view in my Ember.js application. 我在Ember.js应用程序中有一个自定义视图。 It looks like the following: 看起来如下:

App.FocusView = Ember.TextField.extend({
  tagName: 'input',

  focusIn: function(){
    console.log("focus in...");
  },
  focusOut: function(){
    console.log(this.value);
    this.get('controller').send('getSalaryLookup', this.value);
  }
});

In my template, I render the view straight forward: 在我的模板中,我直接呈现了视图:

{{view App.FocusView type=number value=pitcherSalary placeholder="enter pitcher salary ..."}}

My controller code is setup to accept the getSalaryLookup action: 我的控制器代码已设置为接受getSalaryLookup操作:

App.OptimalController = Ember.Controller.extend({
  actions: {
    getSalaryLookup: function(pitcherSalary){
      console.log("This never hits for some reason..");
    }
  }
});

The problem is I never see the message in my controller hit the console. 问题是我再也看不到控制器中的消息了。 I thought I was passing the event off proper, but it doesn't appear to be the case. 我以为我正在适当地传递事件,但事实并非如此。 Is there an obvious error here? 这里有明显的错误吗?

The controller isn't generated when you "dynamically" create a view in your template, it uses the controller that's in scope of the view (you could see this by doing {{log controller}} in the same vicinity of the {{view ...}} , which will send the controller instance to the console). 当你“动态”创建模板视图不产生控制器,它使用的是在考虑范围控制器(你可以做看这个{{log controller}}在同一附近{{view ...}} ,它将控制器实例发送到控制台。

But, in this case, the naming isn't really appropriate because TextField is actually extending a component (which deep down is a view, but still, it's really a component). 但是,在这种情况下,命名并不是真正合适的,因为TextField实际上是扩展了一个组件(其内部是一个视图,但实际上它实际上是一个组件)。 The way you send an action out of a component is you need to subscribe to the action when you declare it, and from the component you use sendAction instead of send . 从组件发送动作的方式是,您需要在声明动作时订阅该动作,然后从组件中使用sendAction而不是send

App.FocusItComponent = Ember.TextField.extend({
  tagName: 'input',

  focusIn: function(){
    console.log("focus in...");
  },
  focusOut: function(){
    var value = this.get('value');
    this.sendAction('getSalaryLookup', value);
  }
});

Here you see I'm subscribing to the getSalaryLookup action with the action name doit . 在这里,你看我订阅的getSalaryLookup动作名称动作doit When the action is sent out of the component it will hit the controller first, if it doesn't find an action there it will hit the route, then up the route tree. 当动作从组件中发出时,它将首先命中控制器,如果在那里没有找到动作,它将命中路线,然后到达路线树。 http://emberjs.com/guides/templates/actions/ . http://emberjs.com/guides/templates/actions/

The reason you specifically subscribe is because you can 1. ignore actions you don't care about from components and 2. have multiple components in the same scope that goes to different actions. 您之所以特别订阅,是因为您可以1.从组件中忽略不需要的操作,并且2.在同一作用域中具有多个可以执行不同操作的组件。

{{focus-it type='number' value=pitcherSalary getSalaryLookup='doit' placeholder="enter pitcher salary ..."}}

Example: http://emberjs.jsbin.com/kuxilefu/1/edit 示例: http//emberjs.jsbin.com/kuxilefu/1/edit

Naming for a component can be understood here: http://emberjs.com/guides/components/defining-a-component/ 可以在这里了解组件的命名: http : //emberjs.com/guides/components/defining-a-component/

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

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