2

I've been playing around a bit with the new js interop of dart. So far everything was very straight-forward. But one thing I'm not sure about is how to deal with js stuff like this:

MathJax.Hub.Config({
  showProcessingMessages: false,
  showMathMenu: false
  .. many other different options
});

I can translate the MathJax.Hub.Config part:

@JS('MathJax') external MathJaxClass get MathJax;
class MathJaxClass {
  external HubClass get Hub;
}

@JS('MathJax.Hub')
class HubClass {
  external void Config(options);
}

But now I would like to have the options argument of Config function to be a Dart Object. I'm not sure how to do this. The only way, I can get something working is with a Map:

  MathJax.Hub.Config(new JsObject.jsify({
    'showProcessingMessages': false,
    'showMathMenu': false
  }));

But this is surely not ideal. Any ideas?

2
  • What argument(s) does the JS function Hub.Config() expect? Commented Oct 28, 2015 at 15:39
  • @GünterZöchbauer One argument. Just a regular Javascript object with only data. Every key is just a string. Commented Oct 28, 2015 at 15:42

2 Answers 2

4

The syntax is as follows:

@anonymous
@JS()
class Config {
  external bool get showProcessingMessages;
  external bool get showMathMenu;

  external factory Config({bool showProcessingMessages, bool showMathMenu});
}

Here the Config name is not matching any javascript name, so you can name it whatever you want. You can then call it like this:

MathJax.Hub.Config(new Config(
  showProcessingMessages: false,
  showMathMenu: false
));

The object passed to the js function, will be a regular javascript object:

Sign up to request clarification or add additional context in comments.

Comments

2

Since a recent update the @anonymous annotation is used to create JS objects from Dart classes instead of a factory constructor.

@JS()
@anonymous
class Config {
  external bool get showProcessingMessages;
  external set showProcessingMessages(bool value);
  external bool get showMathMenu;
  external set showMathMenu(bool value);
}

MathJax.Hub.Config(new Config()
  ..showProcessingMessages= false
  ..showMathMenu = false
}));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.