Skip to content

Instantly share code, notes, and snippets.

@jamgold
Created November 13, 2014 16:35
Show Gist options
  • Save jamgold/8dae67dca257107b14c6 to your computer and use it in GitHub Desktop.
Save jamgold/8dae67dca257107b14c6 to your computer and use it in GitHub Desktop.
stackoverflow_26863799
<head>
<title>stackoverflow_26863799</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>
<ul>
{{#each stackoverflows}}
<li><a href="#" id="{{_id}}">{{date}}</a> {{_id}}</li>
{{/each}}
</ul>
</template>
Collection = new Meteor.Collection('stackoverflow');
if (Meteor.isClient) {
// counter starts at 0
Session.setDefault("counter", 0);
Session.setDefault('docId', null);
Template.hello.helpers({
counter: function () {
return Session.get("counter");
},
stackoverflows: function() {
return Collection.find({},{sort:{date:1}});
}
});
Template.hello.events({
'click button': function () {
// increment the counter when button is clicked
Session.set("counter", Session.get("counter") + 1);
Session.set('docId',null);
},
'click a': function(e, t) {
var id = $(e.target).attr('id');
Session.set('docId', id);
}
});
Tracker.autorun(function(){
Meteor.subscribe('stackoverflow', Session.get('docId'));
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
if(Collection.find().count() == 0)
{
for(var i=0;i<1000;i++)
{
Collection.insert({
date: new Date()
});
var x = 50000;while(x>0) {
x--;
}
}
}
});
Meteor.publish('stackoverflow', function(id){
var doc;
if(id) doc = Collection.findOne({_id: id});
if(doc)
{
console.log(doc);
return Collection.find({
$and:
[
{_id:{$ne: doc._id}},
{date:{$gte: doc.date}}
]
},{sort:{date:1},limit:10});
}
else return Collection.find({},{sort:{date:1},limit:10});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment