Skip to content

Instantly share code, notes, and snippets.

View dferber90's full-sized avatar

Dominik Ferber dferber90

View GitHub Profile
@dferber90
dferber90 / confirmNavigation.js
Last active August 3, 2018 09:44
Confirming Navigation with Iron Router (canceling route transition)
/*
* Adds a confirmation dialogue when the current route contains unsaved changes.
*
* This is tricky because Iron Router doesn't support this out of the box, and
* the reactivity gets in the way.
* In this solution, redirecting to the current route is abused
* as a mechanism to stop the current transition, which Iron Router has no API
* for. Because the redirect would trigger the onStop hook, we keep track of
* whether to run the onStop hook or not ourselves in
* `skipConfirmationForNextTransition`.
@dferber90
dferber90 / package.json
Created November 13, 2015 06:11
Example of a package.json for a Meteor project.
{
"name": "project",
"private": true,
"scripts": {
"test": "tape 'tests/unit/**/*.js'"
},
"devDependencies": {
"tape": "4.2.2"
}
}
@dferber90
dferber90 / class.es6.js
Last active November 14, 2015 14:03
Class
// ES6
class Tweet {
constructor (doc) {
_.extend(this, doc);
}
}
Tweets = new Mongo.Collection('tweets', {
transform (doc) {
return new Tweet(doc);
@dferber90
dferber90 / eol.es6.js
Last active August 29, 2015 14:22
Enhanced Object Literals
// ES6
Template.lead.helpers({
twitterLink () {
return this.getTwitterLink();
},
weekday (timestamp) {
return moment(timestamp).format(‘dddd’);
}
});
@dferber90
dferber90 / rest.es6.js
Last active August 29, 2015 14:22
Rest Operator in ES6
// ES6
UI.registerHelper('sum', function (...summands) {
summands.pop(); // remove last argument hash (Spacebars.kw)
return summands.reduce((previousValue, currentValue) => {
return previousValue + currentValue;
});
});
@dferber90
dferber90 / rest.es5.js
Last active August 29, 2015 14:22
Rest Operator
// ES5
UI.registerHelper('sum', function (/* arguments */) {
var summands = Array.prototype.slice.call(arguments);
summands.pop(); // remove last argument hash (Spacebars.kw)
var sum = 0;
for (var i = 0; i < summands.length; i++) {
sum += summands[i];
}
return sum;
});
@dferber90
dferber90 / versions2.es6.js
Last active August 29, 2015 14:22
Versions Parsing in ES6, Attempt 2
// ES6
const versionsMap = new Map();
lines.forEach( line => versionsMap.set(...line.split(‘@’)) );
@dferber90
dferber90 / versions1.es6.js
Last active August 29, 2015 14:22
Versions Parsing in ES6, Attempt 1
// ES6
const versionsMap = new Map();
lines.forEach((line) => {
const [name, version] = line.split(‘@’); // destructuring
versionsMap.set(name, version);
});
@dferber90
dferber90 / versions.es5.js
Last active August 29, 2015 14:22
Versions Parsing in ES5
// ES5
var versionsMap = {};
lines.forEach(function (line) {
// split name@version into [name, version]
var lineContents = line.split('@');
var name = lineContents[0];
var version = lineContents[1];
versionsMap[name] = version;
});
@dferber90
dferber90 / arrows.es6.js
Last active August 29, 2015 14:22
arrows in es6
// ES6
Template.stats.onCreated(function () {
this.autorun(() => {
const filter = { limit: Session.get('options.limit') };
this.subscribe('tweets', filter); // same "this" as in line 3
});
});