简体   繁体   English

Ember.js:控制器操作未由组件的“ sendAction”触发

[英]Ember.js: controller actions not triggered by 'sendAction' from a component

I'm facing some issues in an Ember.js application and some sendAction that do not work. 我在Ember.js应用程序中遇到了一些问题,而一些sendAction却无法正常工作。 Basically, I have: - an Input component (the one triggering the action) - one controller/template (that contains the rendering of the component) - one other controller / template, in which we render the previous controller. 基本上,我有:-一个输入组件(一个触发动作的组件)-一个控制器/模板(包含组件的渲染)-一个其他控制器/模板,我们在其中渲染前一个控制器。

Here's the code. 这是代码。

First, the text field component: 首先,文本字段组件:

Tm.JiraCloudInputComponent = Ember.TextField.extend({
  keyDown: function (event) {
    if (event.keyCode === 27) {
      this.sendAction('cancelJiraCloudUrl');
    }

    if (event.keyCode === 13) {
      event.preventDefault();
      this.sendAction('saveJiraCloudUrl');
    }
  },

  blur: function () {
    this.sendAction('saveJiraCloudUrl');
  }
})

The controller that has the actions: 具有操作的控制器:

Tm.JiraCloudController = Ember.Controller.extend({
  jiraCloudEnabled: Ember.computed.oneWay('content.jiraCloudEnabled'),
  jiraCloudUrl: Ember.computed.oneWay('content.jiraCloudUrl'),
  successMessage: '',
  errorMessage: '',

  actions: {
    saveJiraCloudUrl: function () {
      if (Tm.isEmpty(this.get('jiraCloudUrl'))) {
        this.unlinkJiraCloud();
      } else {
        this.linkJiraCloud();
      }
    },

    cancelJiraCloudUrl: function () {
      this.set('jiraCloudUrl', this.get('model.jiraCloudUrl'));
      this.set('jiraCloudEnabled', this.get('model.jiraCloudEnabled'));
      this.clearMessages();
    }
  }
});

and its associated template: 及其关联的模板:

{{#default-box id="jira-cloud-settings" class="box-full-width"}}
  {{box-header title="settings.jira_cloud"}}
  {{#box-content}}
    {{jira-cloud-input class="form-control" value=jiraCloudUrl placeholder="Jira cloud url"}}
    <button type="button" class="btn btn-default" title="Link Jira cloud" {{action "saveJiraCloudUrl"}}>Link Jira cloud</button>
  {{/box-content}}
{{/default-box}}

Note: the boxes (default-box, box-content) are also components. 注意:框(默认框,框内容)也是组件。

And last, the template in which we render the previous controller: 最后,我们用来渲染先前控制器的模板:

{{render "jiraCloud" content}}

I can't find anything obvious why it does not work. 我找不到任何不明显的原因。 No error is raised by the 'sendAction' calls. “ sendAction”调用不会引发任何错误。 Note that the action on the button works like a charm. 请注意,按钮上的操作就像是超级按钮。

Thanks for your help, Vincent 感谢您的帮助,文森特

Well, I've just been stupid in fact. 好吧,我实际上只是很愚蠢。 I was too used to the old "send" so I forgot to do the mapping when rendering the component: 我太习惯了旧的“发送”,因此在渲染组件时忘了做映射:

{{jira-cloud-input save="saveJiraCloudUrl" esc="cancelJiraCloudUrl" class="form-control" value=jiraCloudUrl placeholder="Jira cloud url"}}

And in the code: 并在代码中:

Tm.JiraCloudInputComponent = Ember.TextField.extend({
  keyDown: function (event) {
    if (event.keyCode === 27) {
      this.sendAction('esc');
    }

    if (event.keyCode === 13) {
      this.sendAction('save');
      event.preventDefault();
    }
  },

  blur: function () {
    this.sendAction('save');
  }
})

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

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