[英]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.