javascript - How can I try out the latest JS interop? https://github.com/dart-lang/js-interop -
i'm looking seek out "next gen" dart js interop goal of beingness able export dart libraries utilize js. doesn't seem work me though. followed readme at:
https://github.com/dart-lang/js-interop/blob/master/readme.md
i created new project, set dependency github repo, added class in readme, , tried instantiate class javascript using console.
when using either default or named constructors in console get:
var = new dart.lib.a.withname('word'); typeerror: undefined not function var = new dart.lib.a(); typeerror: undefined not function
is rewrite functional yet? doing wrong? there different branch of js-interop should using?
here's simple project follows readme:
## pubspec.yaml name: lib description: sample dart lib published js dependencies: browser: js: git: git://github.com/dart-lang/js-interop.git transformers: - js - js/initializer ## lib/a.dart library lib; import 'package:js/js.dart'; @export() class { string name; a(); a.withname(this.name); } ## web/index.html <!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>index</title> </head> <body> <p id="text"></p> <script type="application/dart" src="index.dart"></script> <!-- next line work, pubspec.yaml file must have dependency on 'browser' --> <script src="packages/js/interop.js"></script> <script src="packages/browser/dart.js"></script> </body> </html> ## web/index.dart import 'package:lib/a.dart'; import 'package:js/js.dart'; main() { initializejavascript(); var = new a(); } ## generated index.dart_initialize.js window.dart = window.dart || {}; window.dart.object = function dartobject() { throw "not allowed"; }; window.dart.object._wrapdartobject = function(dartobject) { var o = object.create(window.dart.object.prototype); o.__dart_object__ = dartobject; homecoming o; }; // library lib (function (ns) { var lib = ns; ["lib"].foreach(function (s) { lib = lib[s] = lib[s] || {}; }); // class (function(parent) { var constructor = parent.a = function _a() { this.__dart_object__ = constructor._new(); }; constructor.prototype = object.create(dart.object.prototype); constructor.prototype.constructor = constructor; constructor._wrapdartobject = function(dartobject) { var o = object.create(constructor.prototype); o.__dart_object__ = dartobject; homecoming o; }; constructor.withname = function _withname(name) { this.__dart_object__ = constructor._new_withname(name); } constructor.withname.prototype = constructor.prototype; })(lib); })(window.dart);
i tried code , indeed left out of instructions. need have phone call initializejavascript()
in main()
method set exports. also, script tag interop.js
needs before dart script.
once made changes able run code , in js console:
> var = new dart.lib.a.withname('word'); < undefined > < _withname {__dart_object__: dartobject, constructor: function} > a.name < "word" > instanceof dart.lib.a; < true
javascript dart interop
No comments:
Post a Comment