使用jQuery的Dart JavaScript interop回调 [英] Dart JavaScript interop callbacks with jQuery
问题描述
如何将以下jquery代码翻译为Dart?我无法使用js.interop使警报回调正常工作。
How can I translate the following jquery code to Dart? I'm having difficulty getting the alert callback to work using js.interop.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(function () {
$('p').hide('slow', function() {
alert("The paragraph is now hidden");
});
});
</script>
任何帮助。
推荐答案
感谢您的问题!我不确定自己,但事实证明这是可能的。 :)
thanks for your question! I wasn't sure myself, but turns out this is possible. :)
首先,将 js
添加到您的pubspec.yaml:
First off, add js
to your pubspec.yaml:
name: jquerydart
description: A sample application
dependencies:
js: any
然后,通过命令行或Dart编辑器运行 pub install 。
Then, run pub install, either via the command line or via Dart Editor.
然后,在您的Dart文件中:
Then, in your Dart file:
import 'dart:html';
import 'package:js/js.dart' as js;
hideIsDone() {
window.alert('all done!');
}
void main() {
js.scoped(() {
js.context.jQuery('p').hide(1000, new js.Callback.once(() => hideIsDone()));
});
}
注意,要从JS回调到Dart,需要创建一个Callback对象。
Note that to callback from JS into Dart, you need to create a Callback object.
另外请注意,对于jQuery变量,不能使用 $
,因为dart2js也使用 $
。因此,在此期间,您需要在您的Dart代码中使用 jQuery
。
Also note you cannot use $
for the jQuery variable, as dart2js also uses $
. So in the meantime you need to use jQuery
in your Dart code.
说完之后,我们可以通过JS-Dart interop使用jQuery,但是Dart应该为我们做这个。所以我打开了错误 http://code.google.com/p/dart/issues/detail? id = 6526
Having said all that, it's cool that we can use jQuery via JS-Dart interop, but Dart should really do this for us. So I opened bug http://code.google.com/p/dart/issues/detail?id=6526
这篇关于使用jQuery的Dart JavaScript interop回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!