Skip to content

Instantly share code, notes, and snippets.

View jonathanconway's full-sized avatar
⌨️
Typing

Jon jonathanconway

⌨️
Typing
View GitHub Profile
Common.context('#news-page', function($) {
console.log(this.html());
});
// output:
// <div id="#news-page">
// <h2>Australian broadband news and information</h2>
// <p>Friday round-up</p>
@jonathanconway
jonathanconway / gist:652644
Created October 29, 2010 00:33
Load the current querystring parameters into an associative array, taking into account repeated keys
// By JonathanConway.net
// Based off original code by Andy E. http://stackoverflow.com/questions/901115/get-querystring-values-with-jquery/2880929#2880929
Object.prototype.isArray = function () {
/// <summary>
/// Returns true if this object is an array.
/// </summary>
return !(this.push === undefined);
};
@jonathanconway
jonathanconway / jquery-checked-filter
Created November 17, 2010 07:11
A 'checked' filter that actually works
jQuery.extend(jQuery.expr[':'], {
/// <summary>
/// Filter out checkboxes which are checked.
/// </summary>
checked: function(element) {
return element.checked;
}
});
@jonathanconway
jonathanconway / gist:709722
Created November 22, 2010 09:23
ASP.NET MVC - Output full-HTML validation errors
public static class ValidationExtensions
{
public static MvcHtmlString ValidationMessageHtmlFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
bool htmlEncode)
{
return new MvcHtmlString(
HttpUtility.HtmlDecode(
htmlHelper.ValidationMessageFor<TModel, TProperty>(
@jonathanconway
jonathanconway / gist:726593
Created December 3, 2010 04:49
jQuery CSS filter
$.extend($.expr[':'], {
/// <summary>
/// Filters on whether the element has the same css property & value as specified.
/// Example: $('a:css(color:blue)') returns only links which are blue.
/// </summary>
css: function(element, index, params) {
var style = params[3].split(':');
return element.style[style[0]] === style[1];
}
});
@jonathanconway
jonathanconway / gist:734237
Created December 9, 2010 02:15
between() method for number types
Number.prototype.between = function(n1, n2) { return this >= n1 && this <= n2; }
@jonathanconway
jonathanconway / gist:735792
Created December 10, 2010 04:37
The times() method - a more pleasant way to do for-loops in Javascript
Number.prototype.times = function(fn) { for (var i = 0; i < this; i++) { fn(i); } }
@jonathanconway
jonathanconway / jquery.prebind.js
Created June 25, 2011 05:53
preBind() - Add an event binding *before* any pre-existing bindings. (An early version, may not work in all scenarios)
$.fn.preBind = function(type, data, fn) {
var currentBindings = this.data('events')[type];
var currentBindingsLastIndex = currentBindings.length - 1;
var newBindings = [];
// bind the event
this.bind(type, data, fn);
// move the new event to the top of the array
newBindings.push(currentBindings[currentBindingsLastIndex]);
@jonathanconway
jonathanconway / gist:1263611
Created October 5, 2011 04:09
InsertGenerator stored proc (which can escape single-quotes)
CREATE PROC InsertGenerator
(@tableName varchar(100)) as
--Declare a cursor to retrieve column specific information
--for the specified table
DECLARE cursCol CURSOR FAST_FORWARD FOR
SELECT column_name,data_type FROM information_schema.columns
WHERE table_name = @tableName
@jonathanconway
jonathanconway / TypeExtensions.IsSimpleType.cs
Created August 12, 2012 08:09
Determine whether a type is simple.
public static class TypeExtensions
{
/// <summary>
/// Determine whether a type is simple (String, Decimal, DateTime, etc)
/// or complex (i.e. custom class with public properties and methods).
/// </summary>
/// <see cref="http://stackoverflow.com/questions/2442534/how-to-test-if-type-is-primitive"/>
public static bool IsSimpleType(
this Type type)
{