Skip to content

Instantly share code, notes, and snippets.

View fjeldstad's full-sized avatar
🦄

Anders Fjeldstad fjeldstad

🦄
View GitHub Profile
private static HtmlDocument GetHtmlDocument(string url)
{
var page = new HtmlDocument
{
OptionOutputAsXml = true
};
HtmlNode.ElementsFlags.Remove("option");
// Fetch and load HTML document (default gym selected)
var request = (HttpWebRequest)HttpWebRequest.Create(url);
@fjeldstad
fjeldstad / gist:1730281
Created February 3, 2012 13:56
Simple UrlHelper extension for rendering app-relative content urls with last-modified timestamp appended as a querystring parameter.
using System;
using System.IO;
using System.Web.Mvc;
using System.Web;
namespace Example.Web.Helpers
{
public static class UrlHelperExtensions
{
/// <summary>
@fjeldstad
fjeldstad / gist:2428788
Created April 20, 2012 13:45
Idea for a utility method that maps ASP.NET MVC ModelState to "_ValidationErrors" observable array properties on a Knockout view model.
(function (lab, $, undefined) {
lab.Utils = new (function () {
return {
UpdateValidationErrors: function (viewModel, modelState) {
var keySeparator = new RegExp("[.\\[\\]]+");
for (var key in modelState) {
if (!modelState[key]) {
continue;
}
@fjeldstad
fjeldstad / gist:2469405
Created April 23, 2012 07:50
Simple "type-stamp" model binder that can bind complex, abstract objects provided they implement a specific interface ITypeStamped
public class TypeStampedModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (typeof(ITypeStamped).IsAssignableFrom(bindingContext.ModelType))
{
// Handle type-stamped model
var prefix = bindingContext.ModelName;
if (bindingContext.ValueProvider.ContainsPrefix(prefix))
{
@fjeldstad
fjeldstad / gist:2490355
Created April 25, 2012 14:56
ModelStateDictionary.Simplify extension method
/// <summary>
/// Simplifies the structure of a ModelStateDictionary instance to a "key => [ "error1", "error2", ... "errorN" ]" dictionary.
/// Also removes entries with no error messages.
/// </summary>
/// <param name="modelState"></param>
/// <returns></returns>
public static Dictionary<string, string[]> Simplify(this ModelStateDictionary modelState)
{
return modelState.ToDictionary(
entry => entry.Key,
@fjeldstad
fjeldstad / gist:2490381
Created April 25, 2012 15:00
SetValidationResult helper function for updating a Knockout view model with validation errors from a simplified modelstate dictionary
SetValidationResult: function (viewModel, modelState) {
var trySetErrors = function (modelStateKey, modelStateErrors) {
var keySeparator = new RegExp("[.\\[\\]]+");
var propertyChain = modelStateKey.split(keySeparator);
var currentObject = viewModel;
var currentPropertyName = propertyChain.shift();
var targetObject = null;
while (currentObject && currentObject[currentPropertyName]) {
@fjeldstad
fjeldstad / gist:2490390
Created April 25, 2012 15:01
Extension method for Knockout that enables an Errors collection on observables
(function (ko, undefined) {
ko.observable.fn.withValidation = function () {
var observable = this;
observable.Errors = ko.observableArray();
observable.HasErrors = ko.computed(function () {
return observable.Errors().length > 0;
});
return observable;
};
@fjeldstad
fjeldstad / notifications-dynamics-crm-2011.js
Last active August 29, 2015 14:00
Shims for form-level notifications in Microsoft Dynamics CRM 2011 (pre/post UR12) compatible with the notifications API from CRM 2013.
// Notification shims for Dynamics CRM 2011 (both pre and post UR12).
// The API is identical to the native (supported) notifications in
// Dynamics CRM 2013. Please note that calling undocumented JavaScript functions
// is not officially supported by Microsoft, which makes these shims
// "unsupported". But since the API is compatible with 2013 the upgrade path
// is straightforward (just remove the code completely, or leave them if you
// prefer - either should be fine).
if (typeof(Xrm.Page.ui.clearFormNotification) === "undefined") {
Xrm.Page.ui.clearFormNotification= function(messageId) {
var crmNotifications =
@fjeldstad
fjeldstad / adal-app.js
Last active October 24, 2019 15:57
Example usage of login popup with adal.js
var authContext = new AuthenticationContext({
// ...
});
// Our custom asynchronous login function. Uses authContext.config.displayCall
// to show the Azure AD login page in a popup window, then periodically checks
// the popup window for the resulting hash + uses adal.js to handle it.
// For this to work, we need a dummy landing page to use as redirectUri.
// This can be an empty HTML page, but needs to have the same origin as the
// main window.
@fjeldstad
fjeldstad / Handler.cs
Last active September 26, 2015 21:06
public interface IHandler<TMessage>
{
string StateKey(TMessage message);
HandlerResult Handle(TMessage message, object state);
}
public class HandlerResult
{
public object State { get; set; }
public IEnumerable<object> Messages { get; set; }