简体   繁体   English

在rpc之后让mathjax重新加载页面

[英]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: JavaScript部分:

<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. 我只需要知道是否以及如何告诉MathJax重新加载页面即可。.我找不到能做到这一点的示例。 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 : MathJax文档中

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. 这将导致MathJax在下次可以排版时对页面进行排版。 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. 它保证排版将与jax,扩展名,字体,样式表和其他异步活动的加载正确同步,并且是要求MathJax处理其他材料的唯一真正安全的方法。

The MathJax.Hub.Typeset() command also accepts a parameter that is a DOM element whose content is to be typeset. MathJax.Hub.Typeset()命令还接受参数,该参数是要设置其内容的DOM元素。 That could be a paragraph, or a element, or even a MathJax math tag. 那可能是一个段落,一个元素,甚至是MathJax数学标记。 It could also be the DOM id of such an object, in which case, MathJax will look up the DOM element for you. 也可以是此类对象的DOM ID,在这种情况下,MathJax将为您查找DOM元素。 So 所以

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

would typeset the mathematics contained in the element whose id is MathExample. 将排版id为MathExample的元素中包含的数学。

How I implemented it (using Google Web Toolkit): 我的实现方式(使用Google Web Toolkit):

JavaScript function: JavaScript函数:

<!-- 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() : 通过声明本机方法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();
      }
    });
}

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

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