Meteor Template Update

I am new to Meteor. I built a simple test and stuck on a very simple part. The content of my webpage is updated only when the db changes, but how can I make it refresh when the function returns the results of the changes? Here is the code part of my application:

client.js:

Meteor.subscribe("tasks"); var search_query = ""; Template.page.tasks = function() { return Tasks.find({title: {$regex: search_query}}); }; Template.task.events({ 'click .checkmark': function() { Tasks.update({_id: this._id}, {$set: {done: !this.done}}); } }); Template.page.events({ 'keyup .search': function() { search_query = $(".search").val(); } }); 

todo.html:

 <head> <title>Todo list</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> {{> page}} </body> <template name="page"> <div class="tasks"> <h1>Collections TODOs</h1> <input type="text" class="search" placeholder="Search..."> {{#each tasks}} {{> task}} {{/each}} </div> </template> <template name="task"> <div class="task well well-small"> <button type="button" class="close cancel">&times;</button> <h3> {{#if done}} <button type="button" class="checkmark done">&#10003;</button> {{else}} <button type="button" class="checkmark muted">&#10003;</button> {{/if}} {{title}} </h3> <div class="creator muted">{{creator}}</div> <p>{{description}}</p> </div> 

So now I expect that whenever a user enters something in the input.search variable, the search_query variable changes, and now Template.page.tasks returns different results. But nothing really gets updated. You can watch the application at http://slava.meteor.com

+7
source share
1 answer

For dynamic re-search, you will need a reactive variable . Make a search_query a Session variable like this

 Template.page.tasks = function() { return Tasks.find({title: {$regex: Session.get("search_query") }}); }; 

And in your case:

 Template.page.events({ 'keyup .search': function() { Session.set("search_query", $(".search").val()); } }); 
+8
source

All Articles