Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First add the <code>js</code> dependency to your <em>pubspec.yaml</em> :</p> <pre class="lang-yaml prettyprint-override"><code>dependencies: js: any </code></pre> <p>By using <em>js-interop</em> you can write almost the same code as in <em>javascript</em>.</p> <pre><code>import 'dart:html'; import 'package:js/js.dart' as js; void main() { js.scoped(() { js.context.$(new js.Callback.once(($) { $('p').hide('slow', new js.Callback.once(() { js.context.alert("The paragraph is now hidden"); })); })); }); } </code></pre> <p>The main differences are :</p> <ul> <li>You have to use <code>js.Callback.once</code> or <code>js.Callback.many</code> to set your callback functions. Use <code>js.Callback.once</code> if your callback is call only one time.</li> <li>Your code must be wrapped with the <code>js.scoped</code>. Basically, <a href="http://www.dartlang.org/articles/js-dart-interop/#managing-proxy-lifetimes" rel="noreferrer">managing proxy lifetimes</a> is here to prevent memory leak.</li> </ul> <p>That said, you can simplify the above code :</p> <pre><code>import 'dart:html'; import 'package:js/js.dart' as js; void main() { js.scoped(() { js.context.$('p').hide('slow', new js.Callback.once(() { window.alert("The paragraph is now hidden"); })); }); } </code></pre> <p>The changes are :</p> <ul> <li><code>js.context.$(new js.Callback.once(($) {</code> isn't needed because <code>main</code> is equivalent to the jQuery <code>$(function)</code>.</li> <li><code>js.context.alert</code> has been replace by <code>window.alert</code> : it's more efficient to directly use <em>DART</em> functions instead of communicate with <em>JS</em>.</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload