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 am using Dart and its JS interop. I need to convert the following JavaScript code to Dart:

ID3.loadTags("filename.mp3", function() {
  var tags = ID3.getAllTags("filename.mp3");
  if (tags.artist)
    artist.textContent = tags.artist;
  if (tags.title)
    track.textContent = tags.title;
}, {
  dataReader: FileAPIReader(file)
});

Note the anonymous callback as the second parameter to loadTags. How do I create that with Dart and the dart:js library?

The closest I got was creating a named function with:

  js.context['loadTagsCallback'] = () {
    var tags = ID3.callMethod('getAllTags', ["filename.mp3"]);
    var artistTag = tags['artist'];
    var titleTag = tags['title'];

    if (artistTag != null) {
      artist.text = artistTag;
    }

    if (titleTag != null) {
      track.text = titleTag;
    }
  };

And then using this Dart code:

ID3.callMethod('loadTags', [
    "filename.mp3",
    js.context['loadTagsCallback'],
    new js.JsObject.jsify({'dataReader': id3FileReader})
]);

However, I don't want to create the named function. Any ideas or tips?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

Dart closures are automatically converted to JS closures when sent across the border. You can just do this:

ID3.callMethod('loadTags', ["filename.mp3", () {
    var tags = ID3.callMethod('getAllTags', ["filename.mp3"]);
    var artistTag = tags['artist'];
    var titleTag = tags['title'];

    if (artistTag != null) {
      artist.text = artistTag;
    }

    if (titleTag != null) {
      track.text = titleTag;
    }
  },
  new js.JsObject.jsify({'dataReader': id3FileReader})
]);
share|improve this answer

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.