Meteor:在从mongodb渲染内容后应用函数

Meteor:在从mongodb渲染内容后应用函数,mongodb,meteor,sage,Mongodb,Meteor,Sage,我正在使用sage cell将html转换为数学内容 Template.home.rendered = function(){ \\ apply sagecell and mathjax } 但是,呈现的内容来自mongo,因此有时会在sage单元格应用于mongo之后加载。我可以这样做 Template.home.rendered = function(){ Deps.autorun(function(){ if (Content.findOne({_id: ...})){

我正在使用sage cell将html转换为数学内容

Template.home.rendered = function(){
  \\ apply sagecell and mathjax
}
但是,呈现的内容来自mongo,因此有时会在sage单元格应用于mongo之后加载。我可以这样做

Template.home.rendered = function(){
  Deps.autorun(function(){
    if (Content.findOne({_id: ...})){
      \\ apply sagecell and mathjax
    }
  });
}

它更好,但仍然不能一直工作。是否有其他东西可用于检测内容是否完全呈现?

使用新响应编辑:

<template name='pendingAnswer'>
    The answer to your question, coming back whenever, is:
    {{>answer}}
</template>

<template name='answer'>
    {{fromSage}}
</template>     

Template.answer.helpers({ 
    fromSage: function () {  
        Session.get('fromSage');
    }
});

Invoked whenever - from a button, from navigating to the page, on blur...        
function GetAnswerFromSage(data) {
        callHTTP(website,data, callbackFromSage)
}        

function callbackFromSage(err, data) {
        if (err) then log(err);
        Session.set('fromSage', data);
        }
剧本

<script type='text/javascript' src="http://sagecell.sagemath.org/static/embedded_sagecell.js"></script>

不幸的是,这会使网站速度慢得多。

您是否建议我将makeNoise()添加到内容文档中?但这与检查findOne是否返回了非null的内容是一样的,不是吗?因为每个文档都来自mongo,您的构造函数将被调用。这就是我建议您执行转换的时候(除非您希望某些东西变得懒惰)。当它发生变化时,你完全可以控制它,反应性确保它被及时呈现。要组织代码,将其添加到一个或多个makeNoise方法可能是有意义的。这不是将其添加到文档集合。您需要调用mongo更新来执行此操作。动物只是你的组织代表,一个聪明的模型。问题是我无法控制转换何时完成。我使用的是通过使用会话提供的外部api,听起来还可以。一旦保存,任何依赖的html都将更新。直到……才所以当你需要的时候总是这样。在您的问题中,当呈现模板时,您正在做一些事情。相反,当你有完整的数据时,做点什么。是的,这是我应该做的。当我这样做的时候,我发现了错误所在:定义sagecell的脚本(不是运行sagecell的脚本)必须在加载文本后运行。详见我发布的答案
<script type='text/javascript' src="http://sagecell.sagemath.org/static/embedded_sagecell.js"></script>
Template.content.rendered = function(){
  // sage
  Deps.autorun(function(){
    if (Session.get('contentChanged')){
      // loading this script causes mathjax to run
      $.getScript("http://sagecell.sagemath.org/static/embedded_sagecell.js", function(d, textStatus){
        if (textStatus=='success'){
          // this converts <div class='compute'> to a sage cell
          sagecell.makeSagecell({
            inputLocation: 'div.compute',
            evalButtonText: 'Evaluate',
            hide: ['editorToggle']
          });
        }
      })
    }
  })
Template.content.events({
'click a': function(evt){
  evt.preventDefault();
  location.href = evt.currentTarget.href;
}
})