从 Dart 代码调用匿名 Javascript 函数
Calling anonymous Javascript function from Dart Code
我正在编写一个从 JavaScript 调用的 Dart 库,完成后,Dart 需要调用位于 JavaScript 中的匿名回调函数。
Java脚本:
function main() {
application.log("ready");
}
function getValueById(id){
return document.getElementById(id).value;
}
function contact(){
application.log("Contact Submit Clicked");
application.contact({
name: getValueById("contactFormName"),
email: getValueById("contactFormEmail"),
phone: getValueById("contactFormPhone"),
message: getValueById("contactFormMessage"),
subject: "Message From " + getValueById("contactFormName") + " via Contact Form",
callback: function(response){
// TODO: handle response here
console.log("callback being executed");
console.log(response);
}
});
}
var application = function () {
this.companyId = "5905063580";
this.companyName = "Company XYZ";
this.ready = main;
}
HTML:
....
<button onclick="contact()">Submit</button>
飞镖:
import 'dart:html';
import 'dart:js';
...
contact(ContactRequest request, callback) {
new RPC.submit(request, URL.contactURL, request.companyName, (res) {
ContactResponse response = new ContactResponse();
response.fromJson(res);
print(response.toString());
// prints {"message":"Message Sent", ...}
// do callback here
// context.callMethod(callback, [response]); <-- failing here
// callback(response); <-- also failing
});
}
void main() {
String companyId = null;
String companyName = null;
context['application']['log'] = (String param) {
print(param);
};
context['application']['contact'] = (JsObject param) {
ContactRequest request = new ContactRequest();
request.companyId = companyId;
request.companyName = companyName;
request.action = "CONTACT";
request.name = param["name"];
request.email = param["email"];
request.phone = param["phone"];
request.message = param["message"];
request.subject = param["subject"];
contact(request, param["callback"]);
};
...
var application = new JsObject(context['application']);
companyId = application['companyId'];
companyName = application["companyName"];
application.callMethod("ready");
}
Java脚本成功调用 Dart 方法联系,Dart 成功与 Java 后端通信,后端响应 Json,现在最后一步是 Dart 调用发送给它的回调,这是它失败的地方,也是我不知道如何让它工作的地方。
如果我执行回调(响应),这是我得到的异常:
Exception: Uncaught Error: Class 'JsFunction' has no instance method 'call'.
NoSuchMethodError: method not found: 'call'
Receiver: Instance of 'JsFunction'
Arguments: [Instance of 'ContactResponse']
Stack Trace:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:45)
#1 contact.<anonymous closure> (http://localhost:63342/jsapi/web/cloudauctioneers.v1.dart:24:13)
#2 RPC.RPC.submit.<anonymous closure> (package:jsapi/model/rpc.dart:10:17)
#3 _RootZone.runUnary (dart:async/zone.dart:1155)
#4 _Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:484)
#5 _Future._propagateToListeners (dart:async/future_impl.dart:567)
#6 _Future._completeWithValue (dart:async/future_impl.dart:358)
#7 _Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:412)
#8 _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:41)
#9 _asyncRunCallback (dart:async/schedule_microtask.dart:48)
#10 _handleMutation (dart:html:41819)
undefined:1undefined
这也失败了:JsFunction.callMethod(callback, [response]);
Exception: Uncaught Error: No static method 'callMethod' declared in class 'JsFunction'.
NoSuchMethodError: method not found: 'callMethod'
Receiver: Type: class 'JsFunction'
Arguments: [Instance of 'JsFunction', Instance(length:1) of '_GrowableList']
Stack Trace:
#0 NoSuchMethodError._throwNew (dart:core-patch/errors_patch.dart:173)
#1 contact.<anonymous closure> (http://localhost:63342/jsapi/web/cloudauctioneers.v1.dart:26:16)
#2 RPC.RPC.submit.<anonymous closure> (package:jsapi/model/rpc.dart:10:17)
#3 _RootZone.runUnary (dart:async/zone.dart:1155)
#4 _Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:484)
#5 _Future._propagateToListeners (dart:async/future_impl.dart:567)
#6 _Future._completeWithValue (dart:async/future_impl.dart:358)
#7 _Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:412)
#8 _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:41)
#9 _asyncRunCallback (dart:async/schedule_microtask.dart:48)
#10 _handleMutation (dart:html:41819)
我认为您需要像
那样调用 JavaScript 函数
callback.apply([response]);
你将无法通过这种方式 Response
。您只能传递原始值或原始值的集合,例如:
Map arg = {'message': response.message, 'otherfield': response.otherField};
callback.apply([new JsObject.jsify(arg)]);
我正在编写一个从 JavaScript 调用的 Dart 库,完成后,Dart 需要调用位于 JavaScript 中的匿名回调函数。
Java脚本:
function main() {
application.log("ready");
}
function getValueById(id){
return document.getElementById(id).value;
}
function contact(){
application.log("Contact Submit Clicked");
application.contact({
name: getValueById("contactFormName"),
email: getValueById("contactFormEmail"),
phone: getValueById("contactFormPhone"),
message: getValueById("contactFormMessage"),
subject: "Message From " + getValueById("contactFormName") + " via Contact Form",
callback: function(response){
// TODO: handle response here
console.log("callback being executed");
console.log(response);
}
});
}
var application = function () {
this.companyId = "5905063580";
this.companyName = "Company XYZ";
this.ready = main;
}
HTML:
....
<button onclick="contact()">Submit</button>
飞镖:
import 'dart:html';
import 'dart:js';
...
contact(ContactRequest request, callback) {
new RPC.submit(request, URL.contactURL, request.companyName, (res) {
ContactResponse response = new ContactResponse();
response.fromJson(res);
print(response.toString());
// prints {"message":"Message Sent", ...}
// do callback here
// context.callMethod(callback, [response]); <-- failing here
// callback(response); <-- also failing
});
}
void main() {
String companyId = null;
String companyName = null;
context['application']['log'] = (String param) {
print(param);
};
context['application']['contact'] = (JsObject param) {
ContactRequest request = new ContactRequest();
request.companyId = companyId;
request.companyName = companyName;
request.action = "CONTACT";
request.name = param["name"];
request.email = param["email"];
request.phone = param["phone"];
request.message = param["message"];
request.subject = param["subject"];
contact(request, param["callback"]);
};
...
var application = new JsObject(context['application']);
companyId = application['companyId'];
companyName = application["companyName"];
application.callMethod("ready");
}
Java脚本成功调用 Dart 方法联系,Dart 成功与 Java 后端通信,后端响应 Json,现在最后一步是 Dart 调用发送给它的回调,这是它失败的地方,也是我不知道如何让它工作的地方。
如果我执行回调(响应),这是我得到的异常:
Exception: Uncaught Error: Class 'JsFunction' has no instance method 'call'.
NoSuchMethodError: method not found: 'call'
Receiver: Instance of 'JsFunction'
Arguments: [Instance of 'ContactResponse']
Stack Trace:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:45)
#1 contact.<anonymous closure> (http://localhost:63342/jsapi/web/cloudauctioneers.v1.dart:24:13)
#2 RPC.RPC.submit.<anonymous closure> (package:jsapi/model/rpc.dart:10:17)
#3 _RootZone.runUnary (dart:async/zone.dart:1155)
#4 _Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:484)
#5 _Future._propagateToListeners (dart:async/future_impl.dart:567)
#6 _Future._completeWithValue (dart:async/future_impl.dart:358)
#7 _Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:412)
#8 _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:41)
#9 _asyncRunCallback (dart:async/schedule_microtask.dart:48)
#10 _handleMutation (dart:html:41819)
undefined:1undefined
这也失败了:JsFunction.callMethod(callback, [response]);
Exception: Uncaught Error: No static method 'callMethod' declared in class 'JsFunction'.
NoSuchMethodError: method not found: 'callMethod'
Receiver: Type: class 'JsFunction'
Arguments: [Instance of 'JsFunction', Instance(length:1) of '_GrowableList']
Stack Trace:
#0 NoSuchMethodError._throwNew (dart:core-patch/errors_patch.dart:173)
#1 contact.<anonymous closure> (http://localhost:63342/jsapi/web/cloudauctioneers.v1.dart:26:16)
#2 RPC.RPC.submit.<anonymous closure> (package:jsapi/model/rpc.dart:10:17)
#3 _RootZone.runUnary (dart:async/zone.dart:1155)
#4 _Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:484)
#5 _Future._propagateToListeners (dart:async/future_impl.dart:567)
#6 _Future._completeWithValue (dart:async/future_impl.dart:358)
#7 _Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:412)
#8 _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:41)
#9 _asyncRunCallback (dart:async/schedule_microtask.dart:48)
#10 _handleMutation (dart:html:41819)
我认为您需要像
那样调用 JavaScript 函数callback.apply([response]);
你将无法通过这种方式 Response
。您只能传递原始值或原始值的集合,例如:
Map arg = {'message': response.message, 'otherfield': response.otherField};
callback.apply([new JsObject.jsify(arg)]);