Skip to content

Instantly share code, notes, and snippets.

// Module dependencies
var crypto = require('crypto'),
express = require('express'),
hbs = require('express-hbs'),
fs = require('fs'),
uuid = require('node-uuid'),
Polyglot = require('node-polyglot'),
semver = require('semver'),
_ = require('lodash'),
when = require('when'),
@davidblurton
davidblurton / gist:9440434
Last active August 29, 2015 13:57
I wanted the index page to be a static page (which you can write in the Ghost editor) rather than the list of posts (which you can only edit by changing the template). Hacked the router so the index page redirects to a static page at /index
var frontend = require('../controllers/frontend');
module.exports = function (server) {
/*jslint regexp: true */
// ### Frontend routes
server.get('/rss/', frontend.rss);
server.get('/rss/:page/', frontend.rss);
server.get('/page/:page/', frontend.homepage);
server.get('/blog/', frontend.homepage);
@davidblurton
davidblurton / gist:8520944
Last active January 3, 2016 21:19
Call and apply examples
Array.prototype.max = function() {
return Math.max.apply(Math, this);
};
[1,2,3].max(); // => 3
Array.prototype.all = function() {
return [].every.apply(this, [].slice.call(arguments));
};
@davidblurton
davidblurton / gist:7260533
Created November 1, 2013 03:16
Tried to write merge sort in Clojure. Nearly killed me.
(mergesort [6 4 2 3 5 1])
(defn mergesort [x]
(if (< (count x) 2) x
(let [half (quot (count x) 2)]
(merge (mergesort (take half x)) (mergesort (drop half x))))))
(defn merge[a, b]
(if (empty? a) b
(if (empty? b) a
@davidblurton
davidblurton / gist:5291687
Created April 2, 2013 11:55
Ookii example
using (FolderBrowserDialog browseDialog = new FolderBrowserDialog())
{
if (browseDialog.ShowDialog() == DialogResult.OK)
{
m_Path.Text = browseDialog.SelectedPath;
}
}
@davidblurton
davidblurton / gist:5116002
Created March 8, 2013 12:01
Allow you to use caller member name attribute in .NET 4. Requires the C#5 compiler so all developers must be using VS2012 else it will fail silently.
// ReSharper disable CheckNamespace
namespace System.Runtime.CompilerServices
// ReSharper restore CheckNamespace
{
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
public sealed class CallerMemberNameAttribute : Attribute
{
}
}
@davidblurton
davidblurton / gist:5115943
Created March 8, 2013 11:53
You can override the default event handlers on certain controls in App.xaml.cs. For example, we put the cursor at the end of the text on textbox gotfocus (the default is at the start) and we select the text in a password box.
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
...
protected override void OnStartup(StartupEventArgs e)
{
EventManager.RegisterClassHandler(typeof(TextBox), UIElement.GotFocusEvent, new RoutedEventHandler(TextBox_GotFocus));
@davidblurton
davidblurton / gist:5115922
Created March 8, 2013 11:47
Sets the keyboard focus to the first element in the control's tab order.
InitializeComponent();
Loaded += (sender, e) => MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
@davidblurton
davidblurton / gist:5115878
Last active December 14, 2015 16:29
ViewModel is a base class for all view models. It handles INotifyPropertyChanged using the C#5 caller member name attribute
public abstract class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected void RaisePropertyChanged([CallerMemberName] string propertyName = string.Empty)
{
if (propertyName = string.Empty)
{
throw new NotSupportedException("Cannot raise property changed on an empty property name. Make sure you are using the C# 5 compiler to make CallerMemberName work.")