Skip to content

Instantly share code, notes, and snippets.

@jakejscott
jakejscott / gist:789692
Created January 21, 2011 13:44
Did you know about the jQuery element constructor ninja?
<!doctype>
<html>
<head></head>
<body>
<script src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script>
$(document.body).prepend(
@jakejscott
jakejscott / gist:790142
Created January 21, 2011 18:31
ServiceStackAppHarborMVC3 Web.config
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0">
@jakejscott
jakejscott / gist:811043
Created February 4, 2011 12:16
Readable Gihub Code bookmarklet for firefox
javascript:(function(e,a,g,h,f,c,b,d){if(!(f=e.jQuery)||g>f.fn.jquery||h(f)){c=a.createElement("script");c.type="text/javascript";c.src="http://ajax.googleapis.com/ajax/libs/jquery/"+g+"/jquery.min.js";c.onload=c.onreadystatechange=function(){if(!b&&(!(d=this.readyState)||d=="loaded"||d=="complete")){h((f=e.jQuery).noConflict(1),b=1);f(c).remove()}};a.documentElement.childNodes[0].appendChild(c)}})(window,document,"1.3.2",function($,L){$('pre').css({%20'font'%20:%20'12px%20Monaco,"Courier%20New","DejaVu%20Sans%20Mono","Bitstream%20Vera%20Sans%20Mono",monospace'%20})});
@jakejscott
jakejscott / EmailAddressAttribut.cs
Created February 18, 2011 13:16
EmailAddressAttribute
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
using System.Web.Mvc;
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public sealed class EmailAddressAttribute : DataTypeAttribute, IClientValidatable
{
private static Regex _regex = new Regex(@"^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\
@jakejscott
jakejscott / DateTimeAttribute.cs
Created February 18, 2011 13:17
DateTimeAttribute allows you to validate a string property on a DTO as if it was a date
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public sealed class DateAttribute : DataTypeAttribute, IClientValidatable
{
public DateAttribute() : base(DataType.DateTime)
{
@jakejscott
jakejscott / Parser.cs
Created February 18, 2011 14:19
String extention for converting strings to Dates etc. Assumes that you have already validated the string can covert to that type
using System;
using System.ComponentModel;
public static class Parser {
public static T Parse<T>(this string value) {
// Get default value for type so if string
// is empty then we can return default value.
T result = default(T);
if (!string.IsNullOrEmpty(value)) {
@jakejscott
jakejscott / gist:844064
Created February 25, 2011 16:45
Simple.Data.MongoDb dynamics
namespace Things.IO.Mongo
{
using System;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using NUnit.Framework;
using Simple.Data;
using Simple.Data.MongoDb;
internal static class DatabaseHelper
public static class Slugifyer {
public static string Slugify(this string phrase)
{
string str = phrase.RemoveAccent().ToLower();
str = Regex.Replace(str, @"[^a-z0-9\s-]", ""); // invalid chars
str = Regex.Replace(str, @"\s+", " ").Trim(); // convert multiple spaces into one space
str = str.Substring(0, str.Length <= 45 ? str.Length : 45).Trim(); // cut and trim it
str = Regex.Replace(str, @"\s", "-"); // hyphens
@jakejscott
jakejscott / jQueryImageRotator.js
Created May 13, 2011 11:27
Simple image mouseover rollover
var timeout;
$('.map-img').live('mouseover', function() {
var $this = $(this);
var $us = $this.parent().children();
timeout = setInterval(function() {
if ($us.filter(':visible').length == 1) {
var last = $us.last();
last.fadeIn(function(){
last.siblings().show();
@jakejscott
jakejscott / ApplicationController.cs
Created June 21, 2011 09:32
Override asp.net mvc JsonResult with the fast ServiceStack JsonSerializer
public abstract class ApplicationController : Controller
{
protected ActionResult RespondTo(Action<FormatCollection> format) {
return new FormatResult(format);
}
protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior) {
return new ServiceStackJsonResult {
Data = data,
ContentType = contentType,