Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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('evnetName', 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.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Can you try to just pass it as a parameter to Dart and call it with param.apply([])

share|improve this answer
    
param.apply() doesn't work. Uncaught NoSuchMethodError: Cannot call "apply$0" on "#<JsFunction>" (Object #<JsFunction> has no method 'apply$0') – kzhdev Mar 29 '14 at 18:26
    
Try param.apply([]) – Günter Zöchbauer Mar 30 '14 at 13:48
1  
That worked. Thanks for your help. – kzhdev Mar 30 '14 at 14:37

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']]);
share|improve this answer
    
I added more information in my question. Your method won't work because 1. dart didn't know the callback function name. 2. there might not be a function name at all, like the inline function in the question. – kzhdev Mar 29 '14 at 17:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.