Skip to content

Instantly share code, notes, and snippets.

Avatar
:shipit:
Always Be Shipping

Martin H. Normark martinnormark

:shipit:
Always Be Shipping
View GitHub Profile
@martinnormark
martinnormark / CSharpReadFile
Created Jan 24, 2012
Read file contents into a string variable
View CSharpReadFile
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
C# Dynamic extension methods for serializing to XML
View DynamicHelper.cs
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
Method for verifying that a method contains an Attribute
View MvcActionFilterTestHelper.cs
/// <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 Jul 17, 2012
Castle Windsor IoC Container setup for ASP.NET MVC
View BusinessLogicInstaller.cs
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 Sep 5, 2012
Use HtmlHelper from MVC Controller
View html-helper-from-controller.cs
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 Sep 19, 2012
jQuery plug-in for removing a DOM element, when clicking outside it
View jquery.click-outside-to-remove.js
(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 Sep 19, 2012
Bridge jQuery Validate with Globalize, to ensure correct validation for different cultures.
View jquery-globalize-validate.js
// 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 Oct 2, 2012
Trim string for array/collection paths
View trim-input-name.js
// 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 Dec 10, 2015
ViewManager.js for rendering Backbone views asynchronously using any web server framework.
View ViewManager JS.md

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",
View Backbone.CollectionModel.js
(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) {
You can’t perform that action at this time.