简体   繁体   English

流星-反应性更新未渲染的数据

[英]Meteor - Reactively Updating Non-Rendered Data

I'm new to Meteor, and I'm trying to figure out whether its reactive model is flexible for what I'm trying to do. 我是Meteor的新手,我想弄清楚它的反应模型是否可以灵活地用于我想做的事情。 The app I'm making is study aid which basically repeatedly quizzes the user and keeps track of the score on each individual question. 我正在制作的应用程序是学习辅助工具,基本上可以反复测验用户并跟踪每个问题的分数。 The questions are a Mongo collection, and I would like the client to update the DB any time the user answers a question - that is, when a question's "score" property changes. 这些问题是Mongo集合,我希望客户端在用户回答问题时(即,问题的“得分”属性发生更改时)更新数据库。 However, I am not rendering the score. 但是,我没有渲染分数。

To make matters worse, I am rendering the question, but I'm not doing it as a template property - I'm piecing together the template content from one template property which calls out to a JS function. 更糟的是,我正在提出问题,但我没有将其作为模板属性来执行-我将来自一个调用JS函数的模板属性的模板内容拼凑在一起。

Template.bottomText.explainText = function(){
        var page=getPage();
        switch(page){
            case "main":        
                return "Foo text "+question.query+"?";
            case "correct":
                return "You got the question right!  Score "+question.score;
        }
    };

My app is not the typical one-screen Meteor program that I have seen. 我的应用程序不是我所见过的典型的一屏流星程序。 It has basic template sections for the various boxes on the screen, but the content of those boxes is built in JS to comprise the individual views. 它具有屏幕上各个框的基本模板部分,但是这些框的内容是用JS构建的,以包含各个视图。 Therefore, each individual view does not have its own template tag, and hence, the questions and scores on the screen are a product of JS, not templating directly. 因此,每个单独的视图都没有自己的模板标签,因此屏幕上的问题和分数是JS的产物,而不是直接进行模板化。 I tried to have the JS invoke a template to spur reactivity, but I can't seem to get those to render. 我试图让JS调用模板来刺激反应性,但是我似乎无法渲染它们。 When the output for a template specified in JS is returned from the JS, it is returned as a literal, not run through the templating engine. 从JS返回JS中指定的模板的输出时,它以文字形式返回,而不是通过模板引擎运行。 So, no template directly accesses these questions as a template property - the view in which they are rendered is one template property dynamically pieced together in JS. 因此,没有模板作为模板属性直接访问这些问题-呈现它们的视图是在JS中动态拼凑而成的一个模板属性。

So...can I still get reactivity on these question objects? 所以...我还能在这些问题对象上反应吗? If so, how? 如果是这样,怎么办?

First things first.. you Might want to look into the handlebar helper {{#isolate}} , this will allow you control over which template elements get rendered per each reactive update. 首先,您可能想看一下车把帮手{{#isolate}} ,这将使您可以控制每个反应式更新呈现哪些模板元素。

But over all it sounds like you are making your application a bit more complex than it needs to be. 但是总的来说,这听起来像是使您的应用程序变得比所需的要复杂一些。 Please explore docs.meteor.com for ideas and help on how to 'think about' structuring your reactive application. 请浏览docs.meteor.com以获取想法和帮助,以了解如何“思考”构建响应式应用程序。

Its not clear what you are trying to accomplish from your question or what your problem is. 目前尚不清楚您要从问题中解决的问题或问题所在。 You are concerned about parts of the screen re-rendering? 您是否担心屏幕的部分重新渲染? Or are you concerned with hiding the score from the user until you permit them to see the score? 还是在允许用户查看分数之前是否向用户隐藏分数?

If that is the case then you may want to work with a publish/subscribe model ; 如果是这种情况,那么您可能希望使用发布/订阅模型 you will have the client subscribe to the desired data when it is determined they are ready to view it; 确定将要查看数据时,您将让客户端订阅所需的数据; most easily done through the use of meteor Session variables. 使用流星会话变量最容易完成。

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

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