Reputation: 687
I'm trying to convert my project to dart step by step. I have converted a stand alone library to Dart and I'm using the Dart compiled javascript in my project. Thanks for the help in my other question. I'm able to call Dart compiled javascript functions. Now I facing another puzzle. If my Dart function requires a callback, how do I passing a Javascript function into the Dart generated javascript function?
What I am trying to do is register a Dart event handler from Javascript, for example:
in my Dart, I have event bus, dart object can register event handler through:
bus.on('eventName', callbackFunc);
and Dart object fires a event through:
bus.fire('eventName', data);
I expose the bus to Javascript world, through:
js.context['registerEvent'] = bus.on;
In Javascript, I want to call
registerEvent('someEvent', function() { console.log('JS callback' });
to register an event handler, when Dart object fired an event, the JS callback will be invoked.
Upvotes: 1
Views: 401
Reputation: 657058
Can you try to just pass it as a parameter to Dart and call it with param.apply([])
Upvotes: 2
Reputation: 6218
from your previous question,
js.context["sayGreeting"] = (message) {
print("greeting: $message");
};
// pass js function which is already defined above.
someJsObject.callMethod('someOriginalJsFunction', [js.context['sayGreeting']]);
Upvotes: 1