在灰烬与闭合动作,你可以通过一个函数,就像这样: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