Call anonymous Javascript function from Dart Code

I am writing a Dart library, which is called from JavaScript, and upon completion, Darth needs to call the anonymous callback function, which is inside JavaScript.

JavaScript:

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>

Dart:

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");
}

JavaScript successfully makes contact with the Dart method, Dart successfully communicates with the Java Backend that Json answers, now the last step is for Dart to call the callback that was sent to it, this is where it does not work and where I cannot understand how to make it work.

if I make a callback (response), this is the exception that I get:

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

This also fails: 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)
+4
source share
1 answer

, JavaScript,

callback.apply([response]);

Response . , :

Map arg = {'message': response.message, 'otherfield': response.otherField};
callback.apply([new JsObject.jsify(arg)]);
+3

All Articles