javascript - Meteor Accessing a single item in a collection -
i new not meteor script in general. having issues program. have collection storing times. want display each time in it's own div. have collection set this.
timeslots = new mongo.collection('times'); timeslots.insert({time: "8:00am"}); timeslots.insert({time: "8:30am"}); timeslots.insert({time: "9:00am"}); timeslots.insert({time: "9:30am"});
a helper function
template.available.helpers({ 'newtime': function(){ return timeslots.find() } });
i want access each time put in own div. div 8:00am 1 8:30 etc.
my template html is
<div class="col-sm-2 available open" id="opentime"> <h2 class="time"> {{#each newtime}} <p>{{time}</p> {{/each}} </h2> <p class="text"></p><br> <p class="text"></p><br> </div>
however have 20 times 20 different divs. 1 how access value in collection. second how should changing template accesses correct div? thank advice , input. sure way off.
just include div in each loop:
<template name = "available"> {{#each newtime}} <div class="col-sm-2 available open" id="opentime"> <h2 class="time"> <p{{time}}</p> </h2> </div> {{/each}} </template>
and include once - shouldn't hard coding bunch of divs, that's kind of work meteor exists you!! :)
this allow reactivity, can things open browser window app , in console write timeslots.insert({time: "9:30am"});
, if insecure , autopublish packages still included, can watch list change in first browser window.
edit: found couple small errors, such in template helper, need timeslots.find({})
parens inside brackets. aslo, in loop, missing parentheses @ {{time}}
also, mindful code have adding times db everytime meteor restarts, lot. should check , see if there in db, , add seed data. use:
if (timeslots.find().count() === 0) { timeslots.insert({ time: '8:30am' }); timeslots.insert({ time: '9:00am' }); }
you can use meteor reset
clear out db in meantime.
i've edited project it's doing think want, it's not complete, it's enough past block at. took out of buttons, can add them back, or better yet, adapt changes own code. close!! file here:
https://www.dropbox.com/s/b162junes4usew4/teetimes-master.zip?dl=0
that should enough started!
Comments
Post a Comment