Skip to content

Instantly share code, notes, and snippets.

@ismyrnow
ismyrnow / AttachOrUpdate.cs
Created October 22, 2011 23:17
AtachOrUpdate extension method for Entity Framework
public static void AttachOrUpdate<T>(this ObjectSet<T> set, ref T entity) where T : class
{
ObjectContext context = set.Context;
string entitySetName = set.EntitySet.Name;
ObjectStateEntry entry;
bool isDetached = true;
if (context.ObjectStateManager.TryGetObjectStateEntry(
context.CreateEntityKey(entitySetName, entity),
@ismyrnow
ismyrnow / gist:1771836
Created February 8, 2012 18:07
Overriding a global function in JS
// Overrides "some_function"
some_function = function () {
var orig_function = some_function;
return function () {
// call the original function
orig_function.apply(this, arguments);
// do other stuff
};
@ismyrnow
ismyrnow / gist:1861094
Created February 18, 2012 21:45
Creating an unobtrusive JS object
(function (window, undefined) {
var privateVariable = null;
var exampleObject = {
publicMethodOne: function () {},
publicMethodTwo: function () {}
};
// Expose exampleObject to the global object
window.exampleObject = exampleObject;
@ismyrnow
ismyrnow / gist:2173458
Created March 23, 2012 18:22
Creating a local unobtrusive JS object
$(function() {
...
var exampleObject = (function() {
var privateVariable = null;
function privateMethod() { ... }
this.publicMethodOne = function() { ... }
this.publicMethodTwo = function() { ... }
@ismyrnow
ismyrnow / gist:2960982
Created June 20, 2012 17:09
Javascript Namespacing
var DED = (function() {
var private_var;
function private_method()
{
// do stuff here
}
return {
@ismyrnow
ismyrnow / GetZoomFromRadius.js
Last active December 12, 2015 10:09
Get WebMercator zoom level given a radius in miles
function getZoomFromRadius(radius) {
// Calculates the target zoom level given a radius in miles
var mapDiv = $("#map");
var mapWidth = mapDiv.width();
var mapHeight = mapDiv.height();
var radiusMeters = radius * 1609.34; // converting miles to meters
var diameter = radiusMeters * 2;
@ismyrnow
ismyrnow / gist:5079663
Created March 4, 2013 03:20
WebAPI Routing Using Action Names
routes.MapHttpRoute(
name: "DefaultApiWithAction",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional },
constraints: new { action = @"^[^\d].*$" }
);
routes.MapHttpRoute(
name: "DefaultApiWithId",
routeTemplate: "api/{controller}/{id}",
@ismyrnow
ismyrnow / leaflet.singletilewmslayer.js
Created April 18, 2013 19:11
Single Tile WMS layer in Leaflet
L.ImageOverlay.WMS = L.ImageOverlay.extend({
defaultWmsParams: {
service: 'WMS',
request: 'GetMap',
version: '1.1.1',
layers: '',
styles: '',
format: 'image/jpeg',
transparent: false,
@ismyrnow
ismyrnow / index.html
Created July 31, 2013 16:17
Leaflet.GroupedLayerControl
<!DOCTYPE html>
<html>
<head>
<title>Basic example</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.2/leaflet.css" />
<!--[if lte IE 8]><link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.2/leaflet.ie.css" /><![endif]-->
<style type="text/css">
@ismyrnow
ismyrnow / google-analytics-amd.js
Last active March 14, 2022 21:32
Google Analytics AMD Module
define(function (require) {
var module;
// Setup temporary Google Analytics objects.
window.GoogleAnalyticsObject = "ga";
window.ga = function () { (window.ga.q = window.ga.q || []).push(arguments); };
window.ga.l = 1 * new Date();
// Immediately add a pageview event to the queue.