[英]Ember.js: controller actions not triggered by 'sendAction' from a component
我在Ember.js应用程序中遇到了一些问题,而一些sendAction却无法正常工作。 基本上,我有:-一个输入组件(一个触发动作的组件)-一个控制器/模板(包含组件的渲染)-一个其他控制器/模板,我们在其中渲染前一个控制器。
这是代码。
首先,文本字段组件:
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');
}
})
具有操作的控制器:
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();
}
}
});
及其关联的模板:
{{#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}}
注意:框(默认框,框内容)也是组件。
最后,我们用来渲染先前控制器的模板:
{{render "jiraCloud" content}}
我找不到任何不明显的原因。 “ sendAction”调用不会引发任何错误。 请注意,按钮上的操作就像是超级按钮。
感谢您的帮助,文森特
好吧,我实际上只是很愚蠢。 我太习惯了旧的“发送”,因此在渲染组件时忘了做映射:
{{jira-cloud-input save="saveJiraCloudUrl" esc="cancelJiraCloudUrl" class="form-control" value=jiraCloudUrl placeholder="Jira cloud url"}}
并在代码中:
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.