【问题标题】:Let mathjax reload page after an rpc在 rpc 之后让 mathjax 重新加载页面
【发布时间】:2013-03-23 22:01:17
【问题描述】:

我只想做这样的事情:

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

但是没有调用完成我希望它会做的事情。 感谢您的帮助

【问题讨论】:

    标签: javascript rpc mathjax


    【解决方案1】:

    来自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 的元素中的数学。

    【讨论】:

      【解决方案2】:

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

      JavaScript 函数:

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

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

      【讨论】:

        猜你喜欢
        • 2013-06-23
        • 2011-09-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-08
        • 2019-09-08
        • 1970-01-01
        • 2017-11-01
        相关资源
        最近更新 更多