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 / 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 / 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 / jquery.validate.unobtrusive.dynamic.js
Created January 11, 2013 22:41
Dynamically parse new validation rules on DOM elements
(function ($) {
$.validator.unobtrusive.parseDynamic = function (selector) {
//use the normal unobstrusive.parse method
$.validator.unobtrusive.parse(selector);
//get the relevant form
var form = $(selector).first().closest('form');
if (!form.length) {
form = $(selector).first().find('form');
@martinnormark
martinnormark / WebRole.cs
Created January 11, 2013 08:22
Handle Azure Web Role restarts gracefully, by waiting for current requests to complete.
public override void OnStop()
{
Trace.TraceInformation("OnStop called from WebRole");
var rcCounter = new PerformanceCounter("ASP.NET", "Requests Current", "");
while (rcCounter.NextValue() > 0)
{
Trace.TraceInformation("ASP.NET Requests Current = " + rcCounter.NextValue().ToString());
System.Threading.Thread.Sleep(1000);
}
@martinnormark
martinnormark / PopoverView.js
Last active November 29, 2016 10:58
Backbone implementation of the Twitter Bootstrap Popover view
(function () {
App.Views.PopoverView = Backbone.View.extend({
initialize: function (options) {
_.bindAll(this, "render", "setContent", "show", "hide", "toggle", "destroy", "remove");
this.offsetTop = 30;
this.offsetLeft = 0;
@martinnormark
martinnormark / ModalView.js
Created January 6, 2013 10:29
Backbone implementation of the Twitter Bootstrap Modal view
(function () {
App.Views.ModalView = Backbone.View.extend({
tagName: "div",
template: modalTemplate,
initialize: function (options) {
_.bindAll(this, "render", "show", "hide", "toggle", "setContent");
@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 / 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 / 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 / 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);