在灰燼與閉合動作,你可以通過一個函數,就像這樣:EmberJS 2.7 - 使用閉合動作來調用一個函數VS調用該函數作爲一個動作
<div class="dropzone text-center" {{action (action openFilePicker)}}>
<!-- this calls a function (no quotes!) -->
</div>
而不是調用一個動作,如下所示。
<div class="dropzone text-center" {{action (action 'openFilePicker')}}>
<!-- this calls an action (quotes!) -->
</div>
在(1)背襯此組件的代碼看起來是這樣的:
openFilePicker: function() {
let child = this.$('div:first-child');
child.removeClass('is-dragging');
child.html('<h2>Upload GPX Route</h2><div class="dropzone-body-text"><h5>Drag and Drop here</h5>- or -<h5>click anywhere here</h5></div>');
filepicker.pick({
container: 'modal',
maxSize: (5 * 1024 * 1024),
services: ['COMPUTER']
});
}
這使用JQuery的除去從DOM一些元素,然後調用另一個函數(filepicker。選擇)這是在外部加載的JS文件。
在(2)相同的代碼是一個動作裏面:
actions: {
openFilePicker: function() {
let child = this.$('div:first-child');
child.removeClass('is-dragging');
child.html('<h2>Upload GPX Route</h2><div class="dropzone-body-text"><h5>Drag and Drop here</h5>- or -<h5>click anywhere here</h5></div>');
filepicker.pick({
container: 'modal',
maxSize: (5 * 1024 * 1024),
services: ['COMPUTER']
});
}
}
什麼是這樣做的利弊?我知道(1)不是推薦的方式,但(1)可以更容易地訪問component
中的其他代碼(例如組件的屬性)。像往常一樣,Ember
文檔似乎沒有揭示這一點(不是我可以找到)。我所能找到的是this blog post這使得不鼓勵調用任意函數(否則爲什麼首先要採取行動?)
所以,儘管這個問題可以導致意見,但這不是我所追求的。我想了解這兩種方法之間的區別是什麼?例如,當你在INSIDE函數內部時,從JS運行時的角度來看,它有什麼區別?例如,在這兩種情況下,「這個」都是一樣的嗎?
爲什麼更容易訪問組件的其他代碼? Ember將把你的行爲綁定到你的組件上。 – Lux
如果在要調用另一個函數的組件內部的函數內部(如在您爲該組件使用而加載的動態加載的外部文件中 - 這是插件的常見場景),該怎麼辦。 '這'不再受限制,對吧?在調用嵌套函數(我想!)之前,你必須讓myComponent = this在行動中。 – rmcsharry
@Lux事實證明它不會讓它更容易:)這是我的誤解。 – rmcsharry