简体   繁体   中英

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. 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. 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.

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. 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. I tried to have the JS invoke a template to spur reactivity, but I can't seem to get those to render. 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. 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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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