【问题标题】:Meteor: apply function after rendering things from mongodbMeteor:从 mongodb 渲染东西后应用函数
【发布时间】:2013-09-02 05:56:03
【问题描述】:

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

Template.home.rendered = function(){
  \\ apply sagecell and mathjax
}

但是,渲染的内容来自 mongo,因此有时会在应用 sage cell 后加载。我可以做这样的事情

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

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

【问题讨论】:

    标签: mongodb meteor sage


    【解决方案1】:

    用新的回应编辑:

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

    之前:在检索 mongo 时尝试转换: From Meteor Doc

    // An Animal class that takes a document in its constructor
    Animal = function (doc) {
      _.extend(this, doc);
    };
    _.extend(Animal.prototype, {
      makeNoise: function () {
        console.log(this.sound);
      }
    });
    
    // Define a Collection that uses Animal as its document
    Animals = new Meteor.Collection("Animals", {
      transform: function (doc) { return new Animal(doc); }
    });
    
    // Create an Animal and call its makeNoise method
    Animals.insert({name: "raptor", sound: "roar"});
    Animals.findOne({name: "raptor"}).makeNoise(); // prints "roar"
    

    【讨论】:

    • 您是否建议我将 makeNoise() 添加到内容文档中?但这与检查 findOne 是否返回非空值一样,不是吗?
    • 由于每个文档都来自 mongo,因此将调用您的构造函数。这是我建议您执行转换的时候(除非您想要一些懒惰的东西)。您可以完全控制它何时发生变化,并且反应性确保它被及时呈现。为了组织您的代码,将其添加到一个或多个 makeNoise 方法可能是有意义的。这不是将其添加到文档集合中。您需要调用 mongo 更新来执行该操作。 Animal 只是您的组织代表,一种智能模型。
    • 问题是我无法控制转换何时完成。我正在使用sagecell.sagemath.org 提供的外部 api
    • 使用会话听起来不错。保存后,任何依赖的 html 都会更新。直到。所以总是在你需要的时候。在您的问题中,您在呈现模板时正在做某事。相反,当你的数据完整时做一些事情。
    • 是的,这就是我应该做的。当我这样做时,我发现了错误:事实证明,定义 sagecell 的脚本(不是运行 sagecell 的脚本)必须在加载文本后运行。在我发布的答案中查看详细信息
    【解决方案2】:

    脚本

    <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']
              });
            }
          })
        }
      })
    

    如果我从 1 个内容模板转到另一个内容模板,似乎没有重新渲染任何内容,因此未应用 mathjax。我能想到的唯一解决方法是强制重新加载页面:

    Template.content.events({
    'click a': function(evt){
      evt.preventDefault();
      location.href = evt.currentTarget.href;
    }
    })
    

    不幸的是,这使网站速度变慢了。

    【讨论】:

      猜你喜欢
      • 2019-03-21
      • 1970-01-01
      • 2022-11-15
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-21
      • 2015-07-13
      相关资源
      最近更新 更多