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

时间:2013-04-02 14:38:46

标签: javascript rpc mathjax

我只想做这样的事情:

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");        
  }
});

JavaScript部分:

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

我只需要知道是否以及如何告诉MathJax重新加载页面.. 我找不到那样做的例子。我已经尝试了

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

但没有电话做了我希望它会做的事情。 谢谢你的帮助

2 个答案:

答案 0 :(得分:3)

来自MathJax documentation

  

要排版排版操作,请使用命令

     

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

     

这将导致MathJax在下次能够执行此操作时对页面进行排版。它保证排版将与jax,扩展,字体,样式表和其他异步活动的加载正确同步,并且是要求MathJax处理其他材料的唯一真正安全的方法。

     

MathJax.Hub.Typeset()命令还接受一个参数,该参数是要将其内容排版的DOM元素。这可能是段落,元素,甚至是MathJax数学标记。它也可以是这样一个对象的DOM id,在这种情况下,MathJax会为你查找DOM元素。所以

     

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

     

将排列id为MathExample的元素中包含的数学。

答案 1 :(得分:1)

我是如何实现它的(使用Google Web Toolkit):

JavaScript函数:

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

通过声明本机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();
  }-*/;

}

然后在需要的地方拨打电话:

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();
      }
    });
}