Skip to content

Instantly share code, notes, and snippets.

@BenjaminRH
BenjaminRH / gist:6275907
Created August 20, 2013 00:50
Demonstrating managing a user online status with the event-hooks package
// server
Hooks.onLoggedIn = function (userId) {
Meteor.users.update({_id: userId}, {set: {'profile.status': 'online'}});
}
Hooks.onLoggedOut = function (userId) {
Meteor.users.update({_id: userId}, {set: {'profile.status': 'offline'}});
}
@BenjaminRH
BenjaminRH / gist:5759369
Created June 11, 2013 18:19
Demonstrating subscribing to documents in a collection based on search-terms in Meteor.
// CLIENT
Session.setDefault('search-terms', null); // They haven't searched for anything initially
Deps.autorun(function () {
// This will reactively update the subscription when the value of the search-terms session variable changes
Meteor.subscribe('foobar', Session.get('search-terms'));
});
// SERVER
Meteor.publish('foobar', function (searchTerms) {
var resetFields = function () {
var p = Meteor.user().profile;
$('#profile-name').val(p.name);
$('#profile-country-list').select2('val', p.country);
$('#profile-email').val(Meteor.user().emails[0].address);
$('#profile-paypal-email').val(p.paypal_email);
$('#profile-password').val('');
$('#profile-confirm-password').val('');
}
Template.admin.events =
"click #createPost": () ->
Meteor.render(() ->
"{{> create_news_post}}"
)
@BenjaminRH
BenjaminRH / seeds.js
Last active December 18, 2015 05:59
Demonstrating seeding your website with example data in Meteor
if (MyCollection.find().count() === 0) {
// Insert all of the MyCollection data here
}
Session.setDefault('game-list-position', 0); // The current position for the skip
Session.setDefault('viewport', 10); // The number of games to display in the list
Template.gameList.scrollableList = function () {
return Games.find({}, {
limit: Session.get('viewport'), // Limit the games to the current viewport number
skip: Session.get('game-list-position') * Session.get('viewport') // Skip the right number of games
});
}
@BenjaminRH
BenjaminRH / gist:5725081
Last active December 18, 2015 04:19
Demonstrating doing stuff with Collection data ONLY the first time you receive it, with a few different possible approaches. It will maintain state across hot-code pushes.
Session.setDefault('done-shit', false); // In the beginning, we haven't done shit
Meteor.subscribe('foobar', function () {
// We have recieved the data (either for the first time, or in an update)
if (Session.equals('done-shit', false)) {
// This is the first time we've got it
// Do data manipulation, or something
Session.set('done-shit', true);
}
});
Deps.autorun(function () {
// The current game they're playing
var game = Games.findOne(Session.get('current-game')); // or whatever your equivalent of that is
if (! Session.equals('current-game-version', game.version)) {
// The game's version has updated
Session.set('current-game-version', game.version);
// Alert the user here
}
});
client.addListener('PING', function() {
Fiber(function() {
client.send('PONG');
}).run();
});
<head>
<title>Title</title>
<meta charset="utf-8">
<meta name="robots" content="all">
</head>
<body>
{{> nav_header}}
{{> index}}
</body>