Skip to content

Instantly share code, notes, and snippets.

View fwielstra's full-sized avatar

Freek Wielstra fwielstra

View GitHub Profile
@fwielstra
fwielstra / thread.html
Created June 13, 2011 10:24
A thread display template
<pre>
<div class="thread">
<div class="thread-title">
<a href="#/thread/<%= thread.title %>"><%= thread.title %></a>
</div>
<time><%= thread.postdate %></time>
<div class="thread-author"><%= thread.author %></div>
</div>
</pre>
@fwielstra
fwielstra / app.js
Created June 13, 2011 10:24
Sammy.js - Do a POST on the back-end
this.post('#/thread', function(context) {
$.post('thread', this.params);
this.redirect('#/thread/' + this.params.title);
});
@fwielstra
fwielstra / app-backbone.js
Created June 13, 2011 10:25
The same application in Backbone.js
$(function() {
window.ThreadController = Backbone.Controller.extend({
initialize: function() {
this.threadListModel = new ThreadListModel;
this.threadListView = new ThreadListView;
Backbone.history.start();
},
routes: {
"/" : "listThreads"
@fwielstra
fwielstra / api-test.js
Created June 14, 2011 15:12
An example unit test for our save thread method
var nodeunit = require('nodeunit');
exports['API'] = nodeunit.testCase({
'The show() API method returns a specific thread by title and its replies by threadid': function(test) {
test.expect(4);
// our mock data
var posts = [{post: 'test'}, {post: 'test2'}];
var thread = {_id: '1234', title: 'my thread'};
@fwielstra
fwielstra / JSONParser.scala
Created September 9, 2011 10:54
Parsing a nested JSON object
case class Trip(
deviceId:String,
plannedDeparture: DateTime,
fromStation: String,
plannedDepartureFirstStation:DateTime,
viaStation: Option[String],
toStation: String,
plannedArrivalLastStation: DateTime) {
var util = require("util"),
http = require("http"),
url = require("url"),
fileSystem = require('fs'),
path = require('path');
var responseMap = [
{pattern:/\btest\?test\=true$/, response:"test-ok.xml"},
{pattern:/\btest\?test=true&henk=liev$/, response:"test-ok.xml"},
// 'begins with' test
@fwielstra
fwielstra / mock.js
Created March 2, 2012 10:20
mock.js - NodeJS webservice test double / proxy
/*
Uses latest & greatest NodeJS version (0.7.5), may cause external libraries
like Mu to not work because of a changed package (sys was renamed to util)
Fix manually by going into ./node_modules/mu and replacing all instances of
require('sys') with require('util'). Should just run under earlier versions
of Node as long as you replace the below require('util') with require('sys').
Download dependencies using 'npm install'
Run using node mock.js <port (optional)>
@fwielstra
fwielstra / errorhandling.js
Created December 18, 2012 09:30
wrapError replacement for backbone.js
var sync = Backbone.sync;
Backbone.sync = function(method, model, options) {
if (!options.error) {
options.error = function(method, model, options) {
// custom error handling here.
}
}
};
sync(method, model, options);
@fwielstra
fwielstra / aliases
Created August 16, 2016 07:00
git list shizzle
# log format base
lformat = log --pretty=format:'%Cred%h%Creset %C(bold blue)%an%Creset %Cgreen(%cr) %Creset-%C(yellow)%d%Creset %s'
# formatted, graph, branches
l = !git lformat --graph --branches
# formatted, graph current branch only
lb = !git lformat --graph
# formatted, topo-order (?)
lt = !git lformat --graph --branches --topo-order
# formatted, no pager (use with -n to show last X entries)
lnp = !git --no-pager l
@fwielstra
fwielstra / api.js
Created June 14, 2011 15:02
Our controller refactored
module.exports = function(Thread, Post) {
return {
post: function(req, res) {
new Thread({title: req.body.title, author: req.body.author}).save();
},
list: function(req, res) {
Thread.find(function(err, threads) {
res.send(threads);
});