有没有更好的方法来使用新的 js 0.6.0 包使 Dart class 的方法可从 JS 调用?
Is there a better way to make a method of a Dart class callable from JS with the new js 0.6.0 package?
index.html(头)
<script>
var callDartMethod = function(dartObject) {
return dartObject.fullName();
}
</script>
index.dart
import 'package:js/js.dart';
@Js() // about to being changed to @JS
external String callDartMethod(p);
main() {
final p = Person.create(firstName: 'Günter', lastName: 'Zöchbauer');
print(callDartMethod(p)); // indirect call from JS
// print(p.fullName()); // call from Dart directly
}
@Js() // about to being changed to @JS
class Person {
external String get firstName;
external set firstName(String firstName);
external String get lastName;
external set lastName(String lastName);
external Function get fullName;
external set fullName(Function function);
external factory Person({String firstName, String lastName});
static Person create({String firstName, String lastName}) =>
new Person(firstName: firstName, lastName: lastName)
// works but feels a bit cumbersome
..fullName = allowInteropCaptureThis(fullNameImpl);
static String fullNameImpl(self) => '${self.firstName} ${self.lastName}';
}
简答:没有。
pkg/js
目前专注于让 Dart 应用程序使用 JavaScript 库。
我们希望可以更轻松地将用 Dart 编写的 API 导出给 JavaScript 消费者,但这将在以后实现。
index.html(头)
<script>
var callDartMethod = function(dartObject) {
return dartObject.fullName();
}
</script>
index.dart
import 'package:js/js.dart';
@Js() // about to being changed to @JS
external String callDartMethod(p);
main() {
final p = Person.create(firstName: 'Günter', lastName: 'Zöchbauer');
print(callDartMethod(p)); // indirect call from JS
// print(p.fullName()); // call from Dart directly
}
@Js() // about to being changed to @JS
class Person {
external String get firstName;
external set firstName(String firstName);
external String get lastName;
external set lastName(String lastName);
external Function get fullName;
external set fullName(Function function);
external factory Person({String firstName, String lastName});
static Person create({String firstName, String lastName}) =>
new Person(firstName: firstName, lastName: lastName)
// works but feels a bit cumbersome
..fullName = allowInteropCaptureThis(fullNameImpl);
static String fullNameImpl(self) => '${self.firstName} ${self.lastName}';
}
简答:没有。
pkg/js
目前专注于让 Dart 应用程序使用 JavaScript 库。
我们希望可以更轻松地将用 Dart 编写的 API 导出给 JavaScript 消费者,但这将在以后实现。