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 / CSharpReadFile
Created January 24, 2012 07:56
Read file contents into a string variable
MailDefinition md = new MailDefinition();
md.From = "test@domain.com";
md.IsBodyHtml = true;
md.Subject = "Test of MailDefinition";
ListDictionary replacements = new ListDictionary();
replacements.Add("<%Name%>", "Martin");
replacements.Add("<%Country%>", "Denmark");
string body = String.Empty;
@martinnormark
martinnormark / DynamicHelper.cs
Created May 2, 2012 08:11
C# Dynamic extension methods for serializing to XML
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Xml;
using System.Xml.Linq;
/// <summary>
/// Extension methods for the dynamic object.
/// </summary>
@martinnormark
martinnormark / MvcActionFilterTestHelper.cs
Created May 5, 2012 21:16
Method for verifying that a method contains an Attribute
/// <summary>
/// Verifies the controller action, contains an attribute of the specified attributeType.
/// </summary>
/// <param name="controller">The controller.</param>
/// <param name="action">The action method.</param>
/// <param name="attributeType">Type of the attribute to look for.</param>
/// <returns>Returns true if the attribute was present on the action. Otherwise false.</returns>
public static bool VerifyControllerActionAttribute(this Controller controller, Func<ActionResult> action, Type attributeType)
{
MethodInfo methodInfo = action.Method;
@martinnormark
martinnormark / BusinessLogicInstaller.cs
Created July 17, 2012 09:17
Castle Windsor IoC Container setup for ASP.NET MVC
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;
using MyApp.BusinessLogic.Facades;
namespace MyApp.Web.PresentationLogic.Container
{
public class BusinessLogicInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
@martinnormark
martinnormark / html-helper-from-controller.cs
Created September 5, 2012 14:38
Use HtmlHelper from MVC Controller
public string Test()
{
TextWriter tw = TextWriter.Null;
HtmlHelper<TestViewModel> htmlHelper = new HtmlHelper<TestViewModel>(new ViewContext(ControllerContext,
new RazorView(ControllerContext, "asd", "sdsd", false, null),
new ViewDataDictionary(),
new TempDataDictionary(), tw), new ViewPage());
return htmlHelper.TextBoxFor(m => m.Name).ToString();
}
@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) {