在.callMethod在Dart中返回后如何调用.then?

时间:2014-05-14 21:44:15

标签: javascript dart angular-dart cesium czml

我正在尝试使用Dart将.czml文件加载到Cesium地图中。 javascript代码正常工作,看起来像这样

    var viewer = new Cesium.Viewer('cesiumContainer');
    viewer.extend(Cesium.viewerDynamicObjectMixin);
    viewer.dataSources.removeAll();

    var czmlDataSource = new Cesium.CzmlDataSource();

    czmlDataSource.loadUrl('Vehicle.czml').then(function() {
        viewer.dataSources.add(czmlDataSource);
    });

这是我尝试使用的转换为Dart的代码。

    void drawCzmlStream() {    
        _viewer.callMethod('extend', [context['Cesium']['viewerDynamicObjectMixin']]);
        _viewer['dataSources'].callMethod('removeAll');

        var czmlDataSource = new JsObject(context['Cesium']['CzmlDataSource']);

        czmlDataSource.callMethod('loadUrl', ['Vehicle.czml']).then(displaySource(czmlDataSource));

        print("finished");
    }

    void displaySource(dataSource) {
        _viewer['dataSources'].callMethod('add', [dataSource]);
        print("finished displaying");
    }

代码似乎正确执行,czml文件中的数据被加载并在地图上绘制,并且"完成显示"打印出来。但是,在print语句之后会出现错误,而第二个"完成了#34;永远不会显示。我怀疑它与我如何使用.then调用有关。我正在使用AngularDart以及调用drawCzmlStream()的CesiumController。

这是控制台显示:

finished displaying

Undefined function drawCzmlStream

STACKTRACE:
#0      DynamicClosureMap.lookupFunction.<anonymous closure> (package:angular/core/parser/parser_dynamic.dart:51:11)
#1      CallMember.eval (package:angular/core/parser/eval_calls.dart:51:25)
#2      DynamicExpression.eval (package:angular/core/parser/dynamic_parser.dart:53:30)
#3      DynamicExpression.eval (package:angular/core/parser/dynamic_parser.dart:54:7)
#4      BoundExpression.call (package:angular/core/parser/syntax.dart:59:36)
#5      NgEvent.initListener.<anonymous closure> (package:angular/directive/ng_events.dart:154:39)
#6      _rootRunUnary (dart:async/zone.dart:734)
#7      _rootRunUnary (dart:async/zone.dart:735)
#8      _rootRunUnary (dart:async/zone.dart:735)
#9      _ZoneDelegate.runUnary (dart:async/zone.dart:462)
#10     _onRunUnary.<anonymous closure> (package:angular/core/zone.dart:114:63)
#11     VmTurnZone._onRunBase (package:angular/core/zone.dart:98:16)
#12     _onRunUnary (package:angular/core/zone.dart:114:17)
#13     _ZoneDelegate.runUnary (dart:async/zone.dart:462)
#14     _CustomizedZone.runUnary (dart:async/zone.dart:667)
#15     _BaseZone.runUnaryGuarded (dart:async/zone.dart:582)
#16     _BaseZone.bindUnaryCallback.<anonymous closure> (dart:async/zone.dart:608)

1 个答案:

答案 0 :(得分:2)

谢谢Benjamin,你的建议是正确的。这是最后的飞镖码。

  void drawCzmlStream() {    
      _viewer.callMethod('extend', [context['Cesium']['viewerDynamicObjectMixin']]);
      _viewer['dataSources'].callMethod('removeAll');

      var czmlDataSource = new JsObject(context['Cesium']['CzmlDataSource']);
      var loaded = czmlDataSource.callMethod('loadUrl', ['../czml/Vehicle.czml']);
      loaded.callMethod('then', [(_) => displaySource(czmlDataSource)]);

      print("finished");
  }

  void displaySource(dataSource) {
    _viewer['dataSources'].callMethod('add', [dataSource]);
    print("finished displaying");
  }