简体   繁体   中英

Let mathjax reload page after an rpc

I just want to do something like this:

contentAsync.load("linear_regression", new AsyncCallback<String>() {
  public void onFailure(Throwable caught) {
    content.add(new HTML("<h1>FAIL</h1>something went wrong"));
    caught.printStackTrace();
  }

  public void onSuccess(String result) {

    // after the RPC new mathematical equations were added to the web content
    // so now I invoke the JavaScript function 'testFunc' on the client.       
    MainLayout.jsniAlert("testFunc");        
  }
});

The JavaScript part:

<script type="text/javascript">
    function testFunc() {
        alert('huhu');
        MathJax.... <--! reload page -->
    }
</script>

I just need to know if and how it is possible to tell MathJax to reload the page .. I couldn't find an example that does that. I already tried

        MathJax.Hub.Process();
        MathJax.Hub.Update();
        MathJax.Hub.Reprocess();
        MathJax.Hub.Rerender();

But no call did what I hoped it would do. Thanks for any help

From the MathJax documentation :

To queue the typeset action, use the command

MathJax.Hub.Queue(["Typeset",MathJax.Hub]);

This will cause MathJax to typeset the page when it is next able to do so. It guarantees that the typesetting will synchronize properly with the loading of jax, extensions, fonts, stylesheets, and other asynchronous activity, and is the only truly safe way to ask MathJax to process additional material.

The MathJax.Hub.Typeset() command also accepts a parameter that is a DOM element whose content is to be typeset. That could be a paragraph, or a element, or even a MathJax math tag. It could also be the DOM id of such an object, in which case, MathJax will look up the DOM element for you. So

MathJax.Hub.Queue(["Typeset",MathJax.Hub,"MathExample"]);

would typeset the mathematics contained in the element whose id is MathExample.

How I implemented it (using Google Web Toolkit):

JavaScript function:

<!-- Refresh MathJax syntax                                        -->
<script type="text/javascript">
    function reloadMathJax() {
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
    }
</script>

Call it client-side by declaring the native mehod reloadMathJax() :

public class EW implements EntryPoint {

  final RootPanel root = RootPanel.get("root");

  @Override
  public void onModuleLoad() {
    // ...
  }

  public static final native void reloadMathJax()/*-{
      $wnd.reloadMathJax();
  }-*/;

}

To call it then wherever needed:

public void load(final VerticalPanel target, 
               String file, final Successor success) {

    contentAsync.load(file, new AsyncCallback<String>() {
      public void onFailure(Throwable caught) {
        target.add(new HTML("<h1>FAIL</h1>something went wrong"));
        caught.printStackTrace();
      }

      public void onSuccess(String result) {

        if (result == null) {
          target.add(new HTML(
              "Could not load content from server. (RPC returned null)"));
          return;
        }

        HTMLPanel panel = new HTMLPanel(result);
        success.onSuccess(panel);
        target.add(panel);

        EW.reloadMathJax();
      }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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