简体   繁体   English

使用Dart中的Blockly的addChangeListener()函数(js互操作)

[英]using Blockly's addChangeListener() function from Dart (js interop)

I've been writing some wrapper code to access the Blockly API from Dart, using dart:js. 我一直在编写一些包装器代码,以使用dart:js从Dart访问Blockly API。 All is going smoothly until I need to pass a function to one of Blockly's event registration functions on the JS side. 一切都进行得很顺利,直到我需要将一个函数传递给JS端的Blockly事件注册函数之一为止。

The function in question in Blockly is addChangeListener() (see https://developers.google.com/blockly/installation/code-generators ). Blockly中有问题的函数是addChangeListener() (请参阅https://developers.google.com/blockly/installation/code-generators )。 It takes a 0-arg function as a callback. 它使用0-arg函数作为回调。 So my goal is to wrap a Dart function, pass it over, and have that function get called when the event fires. 因此,我的目标是包装Dart函数,将其传递,并在事件触发时调用该函数。

In Dart: 在Dart中:

var blockly = context['Blockly'];

context['codeChangedCallback'] = (){
   print('This should work, right?');
};  

blockly.callMethod('addChangeListener', [context['codeChangedCallback']]);

When Blockly initializes, I get this stack trace: 当Blockly初始化时,我得到此堆栈跟踪:

堆栈跟踪屏幕截图

I'm hoping someone can point out something obvious that I've missed. 我希望有人能指出我想念的显而易见的事情。

I have a feeling this may have to do specifically with the way blockly is handling the function internally, because I tried passing the function outside of blockly and it worked fine. 我觉得这可能与内部处理函数的方式特别相关,因为我尝试将函数传递给外部,并且效果很好。

The problem is that the addChangeListener() callback has an arity of one, and Dart cares about arity. 问题在于,addChangeListener()回调的Arity为1,Dart关心Arity。 Your call should look like: 您的通话应如下所示:

var blockly = context['Blockly'];

blockly.callMethod('addChangeListener', [(var event) => print('Success!')]);

I think this should work 我认为这应该工作

var blockly = context['Blockly'];

context['codeChangedCallback'] = (){
   print('This should work, right?');
};  

blockly.callMethod('addChangeListener', [(){
   print('This should work, right?');
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM