Skip to content

Instantly share code, notes, and snippets.

View martinnormark's full-sized avatar
:shipit:
Always Be Shipping

Martin Høst Normark martinnormark

:shipit:
Always Be Shipping
View GitHub Profile
@martinnormark
martinnormark / jquery.click-outside-to-remove.js
Created September 19, 2012 07:04
jQuery plug-in for removing a DOM element, when clicking outside it
(function ($) {
$.fn.clickOutsideToRemove = function () {
return this.each(function () {
var $this = $(this);
$(document).on("mouseup.clickOutsideToRemove", function (e) {
if ($this.has(e.target).length === 0) {
@martinnormark
martinnormark / jquery-globalize-validate.js
Created September 19, 2012 08:08
Bridge jQuery Validate with Globalize, to ensure correct validation for different cultures.
// Override jQuery Validate methods to be aware of different cultures.
// This will use Globalize (https://github.com/jquery/globalize) methods.
$.validator.methods.number = function (value, element) {
return this.optional(element) || !isNaN(Globalize.parseFloat(value));
};
$.validator.methods.range = function (value, element, param) {
value = Globalize.parseFloat(value);
@martinnormark
martinnormark / trim-input-name.js
Created October 2, 2012 10:11
Trim string for array/collection paths
// In ASP.NET MVC, input fields are often named e.g. 'Products[5].Name'.
// To get rid of the 'Products[5]' part, use the following.
inputChanged: function (event) {
var regex = new RegExp(/^\w*\[\d\]\./),
$input = $(event.target),
inputName = $input.attr("name");
while (regex.test(inputName)) {
var matches = regex.exec(inputName);
@martinnormark
martinnormark / ViewManager JS.md
Last active December 10, 2015 17:18
ViewManager.js for rendering Backbone views asynchronously using any web server framework.

ViewManager.js - Server-side rendered templates for Single Page Backbone Applications

To render views using the ViewManager, add a getTemplate function to your view. Since the ViewManager returns the jQuery AJAX deferred object, the views gets access to all the callbacks etc.

The WaitSpinnerView has a dependency on spin.js: http://fgnass.github.com/spin.js/

App.Views.CommentEditView = Backbone.View.extend({

	tagName: "div",
@martinnormark
martinnormark / Backbone.CollectionModel.js
Created January 6, 2013 10:27
Backbone Model helpers
(function (Backbone, _) {
Backbone.CollectionModel = Backbone.Model.extend({
constructor: function (attributes, options) {
Backbone.Model.prototype.constructor.apply(this, arguments);
if (this.collections) {
for (var key in this.collections) {
@martinnormark
martinnormark / manually-validate.js
Created January 11, 2013 22:44
Programatically call jQuery validate
validate: function () {
var form = this.$el.is("form") ? this.$el : this.$el.closest("form");
if (!form.is("form")) {
form = this.$el.find("form");
}
return form.data().unobtrusiveValidation.validate();
}
@martinnormark
martinnormark / EmailAddressAttribute.cs
Created January 12, 2013 20:42
Email Address validation for ASP.NET MVC
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
using System.Web.Mvc;
namespace MyApp.Web.PresentationLogic.Validation
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public sealed class EmailAddressAttribute : DataTypeAttribute, IClientValidatable
@martinnormark
martinnormark / countersunk-table-rows.css
Created January 15, 2013 10:31
CSS for rendering table rows, as if they were countersunk in relation to the rows above and below
tr.countersink.top td {
-webkit-box-shadow: inset 0 3px 8px rgba(0, 0, 0, 0.125);
-moz-box-shadow: inset 0 3px 8px rgba(0, 0, 0, 0.125);
box-shadow: inset 0 3px 8px rgba(0, 0, 0, 0.125);
}
tr.countersink td {
background-color: #F7F7F7;
-webkit-box-shadow: inset 0 0px 5px rgba(0, 0, 0, 0.125);
-moz-box-shadow: inset 0 0px 5px rgba(0, 0, 0, 0.125);
@martinnormark
martinnormark / HandleAjaxErrorAttribute.cs
Created January 23, 2013 09:17
Log handled exception in AJAX request to ELMAH
using System.Net;
using System.Web.Mvc;
namespace MyApp.Web.PresentationLogic.ActionFilters
{
public class HandleAjaxErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
if (!filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
@martinnormark
martinnormark / ios-log-output.log
Last active December 11, 2015 19:18
Sample iOS logging output
2013-01-27 10:11:26.113 iOS-logging-experiments[24010:c07] -[BPViewController logContextButtonPressed:]:31 someObject=(
)
2013-01-27 10:11:26.115 iOS-logging-experiments[24010:c07] -[BPViewController logContextButtonPressed:]:34 someObject=(
foo
)
2013-01-27 10:11:26.116 iOS-logging-experiments[24010:c07] -[BPViewController logContextButtonPressed:]:36 someObject=(
foo
)
2013-01-27 10:11:26.138 iOS-logging-experiments[24010:c07] (
0 iOS-logging-experiments 0x000021ee -[BPViewController logContextButtonPressed:] + 302