Manage the reactivity among different visitors - meteor

As a beginner, I'm building some tests and trainings.
For now, I have a Monkeys collection that displays a list of monkeys names and ages. Visitors can add or remove monkeys. When a visitor adds a monkey to the list, I wish it would not be visible to the others until they update the list with an update button.
I tried several ways without finding how to reach this purpose.
What is the best way? Or what have I to learn to do it?
Here is my js file(and, of course, all is entirely reactive for now).
//Monkeys collection subscription is in the waitOn function in the router.js page
//--------------------------------------------------------------------
Template.hello.helpers({
monkeys: function(){
var listMonkeys = Monkeys.find({}, {sort: { age: -1, name: 1}});
return listMonkeys;
}
});
//---------------------------------------------------------------------
Template.hello.events({
// insert new monkey
'submit': function (e) {
e.preventDefault();
var name = e.target.new_name.value;
var age = parseInt(e.target.new_age.value);
Monkeys.insert({"name":name,"age":age}, function(error, result) {
if (error) {alert(error.message);}
});
},
//delete this monkey
'click .delete': function(){
var name = this.name;
Monkeys.remove({"_id":this._id}, function(error, result) {
if (result) {alert(name + " has been deleted");}
if (error) {alert(error.message);}
});
},
//update listMonkeys
'click #updatelistMonkeys': function(){
// ... update function
}
});
//--------------------------------------------------------------------------
//There are only some tests in order to understand how works Meteor startup function
Meteor.startup(function () {
var listMonkeys = Monkeys.find({}, {sort: { age: -1, name: 1}}).fetch();
console.log("Meteor.startup: ");
console.log(listMonkeys);//return a empty array
Tracker.autorun(function(){
var listMonkeys = Monkeys.find({}, {sort: { age: -1, name: 1}}).fetch();
console.log("Tracker.autorun: ");
console.log(listMonkeys)//at first, return a empty array, immediately after,return the filled array
})
});
Here is my html template:
<template name="hello">
<h2>Monkeys forever!</h2>
<button id = "updatelistMonkeys"><img src="images/update-contacts-icon.png" style= "width: 2em;"></button>
{{#each monkeys}}
<p class = "listMonkeys">Name: {{name}} - age: {{age}} <button class="delete">X</button></p>
{{/each}}
<form>
<legend ><b>Add a new Monkey: </b> </legend>
<input type="text" name="new_name">
<input type="number" name="new_age">
<button type="submit">submit</button>
</form>
</template>

You can disable reactivity within your helper by making two modifications, include the reactive: false option and add .fetch() to the end, to force the results to be retuned, not a cursor. Try this:
monkeys: function() {
return Monkeys.find({}, {sort: {age: -1, name: 1}, reactive: false}).fetch();
}
Got this work around from a github issue:
https://github.com/meteor/meteor/issues/771

You can load your data from a server method, this solves some problems you have right now but you still need to dev a notification system for your update button to rerun the server method.
// server/methods.js
Meteor.methods({
getMonkeys: function () {
return Monkeys.find({}, {sort: { age: -1, name: 1}}).fetch();
}
})
//tpl helper
Template.hello.helpers({
monkeys: function(){
Meteor.call("getMonkeys", function(err,monkeys){
if(err){
//do smth with the error
}
return monkeys;
})
}
})

# Brian Shambien: it still reactive. You can see there ==> monkeys.meteor.com
I suppose it's because the variable is in a reactive context and re-evaluated each time it changes.
Template.hello.helpers({
monkeys: function(){
var listMonkeys = Monkeys.find({}, {sort: {age: -1, name: 1}, reactive: false}).fetch();
return listMonkeys;
}
});

Related

Why isn't this template reactive?

Why isn't this reactive? And more importantly how can it be made reactive?
I'd like the data to be saved in Mongo and used in the template. I could use a ReactiveVar or ReactiveDict. Do I need two copies of the data?
Doesn't Suspects.findOne('bruce') return a reactive object already? I tried putting the human answer directly on Bruce, but it didn't trigger an update.
The events fire, log(this) shows bruce's answer was changed, but the template doesn't re-render. What's the good way to do this?
http://meteorpad.com/pad/KoH5Qu7Fg3osMQ79e/Classification
It's Meteor 1.2 with iron:router added:
<head>
<title>test</title>
</head>
<template name="question">
{{#unless isAnswered 'human'}} <!-- :-< I'm not reacting here -->
<div>Sir, are you classified as human?</div>
<button id="no">No, I am a meat popsicle</button>
<button id="smokeYou">Smoke you</button>
{{else}}
<div> Classified as human? <b>{{answers.human}}</b></div>
{{/unless}}
</template>
And the JavaScript:
// Why isn't this reactive?
if (Meteor.isClient) {
Template.question.helpers({
isAnswered: function (question) { // :-< I'm not reactive
var suspect = Template.instance().data;
return (typeof suspect.answers[question] !== 'undefined');
}
});
Template.question.events({
'click #no': function () {
this.answers.human = "No"; // :-< I'm not reactive
console.log(this);
},
'click #smokeYou': function() {
this.answers.human = "Ouch"; // :-< I'm not reactive
console.log(this);
}
});
}
// Collection
Suspects = new Meteor.Collection('suspects');
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
Suspects.upsert('bruce', { quest: 'for some elements', answers: {}});
});
Meteor.publish('suspects', function() {
return Suspects.find({});
});
}
// Iron Router
Router.route('/', {
template: 'question',
waitOn: function() {
return Meteor.subscribe('suspects');
},
data: function() {
return Suspects.findOne('bruce');
}
});
Thanks :-)
The events are not actually updating the reactive data source (the db record). Instead of doing:
Template.question.events({
'click #no': function () {
this.answers.human = "No";
}
});
The event needs to perform a database action, either through a direct update or through a Meteor.call() to a Meteor.method. For example:
'click #no': function(){
Suspects.update('bruce', {'answers': {'human': 'no'}});
}
If you use this pattern, you will also need to set the correct allow and deny rules to permit the update from client code. http://docs.meteor.com/#/full/allow. Methods generally end up being a better pattern for bigger projects.
Also, I'm not sure off the top of my head that Template.instance().data in your helper is going to be reactive. I would use Template.currentData() instead just to be sure. http://docs.meteor.com/#/full/template_currentdata
Very close you just need to use ReactiveVar by the sound of it it pretty much explains what it's :) http://docs.meteor.com/#/full/reactivevar
and here's how to use it
if (Meteor.isClient) {
Template.question.onCreated(function () {
this.human = new ReactiveVar();
});
Template.question.helpers({
isAnswered: function (question) {
return Template.instance().human.get();
}
});
Template.question.events({
'click #no': function (e, t) {
t.human.set('No');
console.log(t.human.get());
},
'click #smokeYou': function(e, t) {
t.human.set('Ouch');
console.log(t.human.get());
}
});
}
UPDATE: if you're using a cursor I usually like to keep it on the template level not on iron router:
if (Meteor.isClient) {
Template.question.helpers({
isAnswered: function (question) {
return Suspects.findOne('bruce');
}
});
Template.question.events({
'click #no': function (e, t) {
Suspects.update({_id: ''}, {$set: {human: 'No'}});
},
'click #smokeYou': function(e, t) {
Suspects.update({_id: ''}, {$set: {human: 'Ouch'}});
}
});
}

One template for 2 thing MeteorJS

I have db with posts and all of them have bollean flagged
I have one template and navigation like (Read, Dont Read).
Problem is that I see all posts (when I must see posts with flagged false or flagged true), and I dont understand why, I think problem in publish/subscribe
Router.configure({
layoutTemplate: 'layout',
loadingTemplate: 'loading',
});
AllPostsController = RouteController.extend({
template: 'timeTable',
waitOn: function() {
return Meteor.subscribe('allPosts');
}
});
readPostController = AllPostsController.extend({
waitOn: function() {
return Meteor.subscribe('readPosts');
}
});
dontreaderPostController = AllPostsController.extend({
waitOn: function() {
return Meteor.subscribe('dontreadPosts');
}
});
Router.map(function() {
this.route('timeTable', {path: '/',
controller: AllPostsController
});
this.route('readPosts', {path: '/read',
controller: readPostsController
});
this.route('dontreaderPosts', {
path: '/dontreader',
controller: dontreaderPostController
});
});
Meteor.publish('allPosts', function(){
return Posts.find({},{ sort: { createdAt: -1 }});
});
Meteor.publish('readPosts', function(){
return Posts.find({read:true},{ sort: { createdAt: -1 }});
});
Meteor.publish('dontreadPosts', function(){
return Posts.find({read:false},{ sort: { createdAt: -1 }});
});
If someone need more code, Just ask me
Anybody help
EDIT : David solved problem for regular tasks. Main problem that I have specific return Posts.find(...) in my tamplate helper.
<template name="timeTable">
{{#if posts_exist_week}}
{{> table posts=week}}
{{/if}}
{{#if posts_exist_month}}
{{> table posts=month}}
{{/if}}
</template>
<template name="table">
<table class="main-table table">
{{#each posts}}
{{> post}}
{{/each}}
</table>
</template>
You solved my problem if I did not have template timeTable (that show posts for last week and month) Because here it Template helper
Template.timeTable.helpers({
week: function() {
//...
return Posts.find({createdAt: {$gte: weekstart, $lt: yesterday}},{ sort: { createdAt: -1 }}); //return posts that was created in this week
},
month: function() {
//...
return Posts.find({createdAt: {$gte: monthstart, $lte: weekstart}},{ sort: { createdAt: -1 }});
}
});
And now you see that if I choose your decision (David) I will have 2 !!
return
first - in router
second - in template helper
I recreated this locally and found that extend causes the parent controller's waitOn to run. So whenever you go to the /read route it will actually activate both subscriptions and you'll end up with all of the documents on your client. A simple fix is to refactor your controllers like so:
PostController = RouteController.extend({
template: 'timeTable'
});
AllPostsController = PostController.extend({
waitOn: function() {
return Meteor.subscribe('allPosts');
}
});
readPostController = PostController.extend({
waitOn: function() {
return Meteor.subscribe('readPosts');
}
});
dontreaderPostController = PostController.extend({
waitOn: function() {
return Meteor.subscribe('dontreadPosts');
}
});
That being said, you don't want to build your app in a way that it breaks when extra subscriptions happen to be running. I would rewrite the controllers to select only the documents that pertain to them. For example:
dontreaderPostController = PostController.extend({
waitOn: function() {
return Meteor.subscribe('dontreadPosts');
},
data: {selector: {read: false}}
});
And now your helpers can use the selector like this:
Template.timeTable.helpers({
week: function() {
var selector = _.clone(this.selector || {});
selector.createdAt = {$gte: weekstart, $lt: yesterday};
return Posts.find(selector, {sort: {createdAt: -1}});
}
});
Also note that sorting in the publish functions may not be useful - see common mistakes.

MeteorJs: Return data Iron:router

Iron router return data is in template but I can't use it.
For example I have db with jobs, where every job has a position (e.g. jobs.position):
ExistJobPostController = RouteController.extend({
layoutTemplate: 'existJob',
data:function() {return Posts.findOne(this.params._id); }
})
Router.map(function() {
this.route('existJob', {
path: '/jobs/:_id',
controller: ExistJobPostController,
});
});
<template name="existJob">
{{position}}
</template>
And nothing happens, I think that it's my fault, but I really can't understand how to fix this.
Can anybody help?
You should first check that the correct data is even being set on your template data context. Here's a quick general summary of how to set the data context and how to access it from various locations:
Router.map(function() {
this.route('index', {
path: '/index',
data: function(){
var obj = {
fname: "Tom",
lname: "Smith"
};
return obj;
}
});
});
Template.index.onRendered(function(){
console.log(this.data.fname);
});
Template.index.events({
'click body': function(e, tmpl){
console.log(tmpl.data.fname);
}
});
Template.index.helpers({
lastName: function(){
return this.lname;
}
});
<template name="index">
You have to use `this` when directly accessing template data from spacebars.
{{this.firstName}}
The following comes from Template.index.helpers:
{{lastName}}
</template>

Meteor.js calling a template.helpers function vs global variable

I am using Reactive-table to display paginated data in my meteor.js app as shown below, yet data displayed in Reactive-table is dependent on on specific user event (Selecting client, project, date range and clicking on the submit button). So I was wondering if it is possible to trigger template.helpers >> myCollection function from the 'submit form' event? OR is it better to define a global variable to store data returned from user query based on the user (client, project, date range selection) then make this global variable the return from the myCollection function?
I have tried researching how to call .helpers function from an template.events event but couldn't find any information. So any help on which approach is better and if calling the .events function is better then how to do that, will be highly appreciated. Thanks.
Below is the code I have in my app:
Template.detailedreport.rendered = function() {
Session.set("dreport_customer", "");
Session.set("dreport_project", "");
Session.set("dreport_startDate", new Date());
Session.set("dreport_endDate", new Date());
$('.set-start-date').datetimepicker({
pickTime: false,
defaultDate: new Date()
});
$('.set-end-date').datetimepicker({
pickTime: false,
defaultDate: new Date()
});
$('.set-start-date').on("dp.change",function (e) {
Session.set("dreport_startDate", $('.set-start-date').data('DateTimePicker').getDate().toLocaleString());
});
$('.set-end-date').on("dp.change",function (e) {
Session.set("dreport_endDate", $('.set-end-date').data('DateTimePicker').getDate().toLocaleString());
});
};
Template.detailedreport.helpers({
customerslist: function() {
return Customers.find({}, {sort:{name: -1}});
},
projectslist: function() {
return Projects.find({customerid: Session.get("dreport_customer")}, {sort:{title: -1}});
},
myCollection: function () {
var now = Session.get("dreport_startDate");
var then = Session.get("dreport_endDate");
var custID = Session.get("dreport_customer");
var projID = Session.get("dreport_project");
Meteor.call('logSummary', now, then, projID, custID, function(error, data){
if(error)
return alert(error.reason);
return data;
});
}
},
settings: function () {
return {
rowsPerPage: 10,
showFilter: true,
showColumnToggles: false,
fields: [
{ key: '0._id.day', label: 'Day' },
{ key: '0.totalhours', label: 'Hours Spent'}
]
};
}
});
Template.detailedreport.events({
'submit form': function(e) {
e.preventDefault();
var now = $('.set-start-date').data('DateTimePicker').getDate().toLocaleString();
var then = $('.set-end-date').data('DateTimePicker').getDate().toLocaleString();
var custID = $(e.target).find('[name=customer]').val();
var projID = $(e.target).find('[name=project]').val();
//Here is the problem as I am not sure how to refresh myCollection function in .helpers
},
'change #customer': function(e){
Session.set("dreport_project", "");
Session.set("dreport_customer", e.currentTarget.value);
},
'change #project': function(e){
Session.set("dreport_project", e.currentTarget.value);
}
});
Template:
<div>
{{> reactiveTable class="table table-bordered table-hover" collection=myCollection settings=settings}}
</div>
Server:
Meteor.methods({
logSummary: function(startDate, endDate, projid, custid){
//Left without filtering based on date, proj, cust for testing only...
return Storylog.find({});
}
});
Template helpers are reactive, meaning that they will be recomputed if their dependencies change. So all you need to do is update their dependencies and then the myCollection helper will be recomputed.
Replace your comment // Here is the problem... with:
Session.set('dreport_endDate', then);
Session.set('dreport_startDate', now);
Session.set('dreport_project', projID);
Session.set('dreport_customer', custID);

Meteor - cursor with limit set isn't reactive

I don't know if this is bug, but when i specify limit, the cursor on the client isn't reactive if some data that match the query already exists in the client collection.
For instance if have limit: 4 and there is already 1 record that match, than
it returns that one record and when next 3 records, which machtes the query, arrives from the server, the cursor isn't reactive (i am expecting
it will be evaluated again and it will return all those 4 records).
I found it because when i uncomment the line where i am fetching all records, my app works (because that cursor will reflect that new data are available). You can see that query is same, only except that limit.
messages = Messages.find(selector, {sort: {created: -1}, limit: MessagesAPI.LIMIT}).fetch();
//Messages.find(selector, {sort: {created: -1}}).fetch());
// if i uncomment the previous line, it works
More code
getMeteorState: function () {
console.log("zde");
var time = this.getParams().time;
var dir = this.getParams().dir;
//TODO: maybe check time and dir validity or let it crash ?
var ready = Session.get("messages-ready");
var params = {sort: MessagesAPI.sort.NEW, dir: dir == "prev" ? MessagesAPI.dir.PREV : MessagesAPI.dir.NEXT};
if (time) {
var d = new Date();
d.setTime(time);
params.date = d;
}
Meteor.subscribe("messages", params, function () {
console.log("ready");
Session.set("messages-ready", true);
});
var messages = [];
if (ready) {
var selector = {};
if (time && dir) {
selector.created = {};
var cond = (dir == "prev" ? "$lt" : "$gt");
var date = new Date();
date.setTime(time);
selector.created[cond] = date;
}
messages = Messages.find(selector, {sort: {created: -1}, limit: MessagesAPI.LIMIT}).fetch();
//console.log(selector);
// when i uncomment this, it will work
//console.log(Messages.find(selector, {sort: {created: -1}}).fetch());
}
return {
messages: messages
};
},
It is reactive.
If I create a default app and mod it like so
Messages = new Mongo.Collection("messages");
if (Meteor.isClient) {
// counter starts at 0
Session.setDefault("counter", 0);
Template.hello.helpers({
counter: function () {
return Session.get("counter");
},
messages: function() {
var messages = Messages.find({},{sort: {text: -1}, limit: 4}).fetch();
return messages;
}
});
Template.hello.events({
'click button': function () {
Session.set("counter", Session.get("counter") + 1);
Messages.insert({text: Session.get("counter")});
}
});
}
and html
<head>
<title>reactive</title>
</head>
<body>
<h1>Welcome to Meteor!</h1>
{{> hello}}
</body>
<template name="hello">
<button>Click Me</button>
<p>You've pressed the button {{counter}} times.</p>
<div>
{{#each messages}}
{{text}}
{{/each}}
</div>
</template>
No problem. Manual insert via Mongo console reactively updates

Resources