Skip to content

Instantly share code, notes, and snippets.

@mikeobrien
mikeobrien / main.coffee
Created February 26, 2012 23:04
The 99%
require.config
paths:
order: '/content/scripts/require/order'
jquery: '/content/scripts/jquery'
define 'underscore', ['order!/content/scripts/underscore.js'], -> _
define 'backbone', ['order!/content/scripts/backbone.js'], -> Backbone
require [
'order!jquery',
public class PostHandler
{
...
public AjaxContinuation Execute(Model request)
{
var updatedModel = Map(_addWidgetService.Add(Map(request)));
// How do I get my updatedModel back to the client?
return AjaxContinuation.Successful();
}
}
@mikeobrien
mikeobrien / Client.coffee
Created February 22, 2012 07:04
Error handling with backbone.js + fubumvc
# Proxies ajax errors to the event aggregator
define 'ajaxerrorproxy', ['jquery', 'postal'], ($, postal) ->
options = {}
$(document).ajaxError (error, xhr, settings, thrownError) ->
json = !xhr.getResponseHeader('content-type').indexOf('application/json')
status = options[xhr.status] ? {}
channel = postal.channel "error.ajax.#{status.alias ? xhr.status}"
channel.publish
status: status.alias ? xhr.status
message: status.message ? thrownError
@mikeobrien
mikeobrien / ExceptionHandlerFilter .cs
Created February 21, 2012 20:55
"Conventional" exception handling in ASP.NET MVC
public class ExceptionHandlerFilter : IExceptionFilter
{
...
public void OnException(ExceptionContext context)
{
// Are you kidding me???
var controllerDescriptor = (ControllerDescriptor)new ReflectedControllerDescriptor(context.Controller.GetType());
var actionDescriptor = (ReflectedActionDescriptor)controllerDescriptor.FindAction(context, context.RouteData.Values["action"].ToString());
if (actionDescriptor.MethodInfo.ReturnType == typeof(JsonResult))
@mikeobrien
mikeobrien / app.coffee
Created February 21, 2012 04:49
Lazy loading backbone collection + infinite scrolling
class LazyCollection extends Backbone.Collection
indexQuerystring: 'index'
index: 1
lastLength: 0
fetch: (options) ->
options or= {}
if options.reset
@index = 1
@lastLength = 0
else
@mikeobrien
mikeobrien / gist:1857212
Created February 18, 2012 03:31
Enabling AMD support for underscore and backbone
define 'underscore', ['path/to/underscore.js'], -> _
define 'backbone', ['underscore', 'jquery', 'order!path/to/backbone.js'], -> Backbone
@mikeobrien
mikeobrien / main.js
Created February 18, 2012 01:50
Bootstrapping data with require.js
require(['data', 'app'], function(data, app) {
return app.start(data);
});
@mikeobrien
mikeobrien / Conventions.cs
Created February 17, 2012 16:41
RESTful exception handling
public class Conventions : FubuRegistry
{
public Conventions()
{
// ...
Policies.ConditionallyWrapBehaviorChainsWith<HandledExceptionBehavior>(x => x.BehaviorChain.Last() is JsonSerializer);
// ...
}
}
@mikeobrien
mikeobrien / Registry.cs
Created February 15, 2012 03:40
Blowin up the asset pipeline yo!
Assets.Alias("jquery").Is("scripts/jquery/jquery-1.7.1.min.js")
.Alias("underscore").Is("scripts/underscore/underscore-min.js")
.Alias("backbone").Is("scripts/backbone/backbone-min.js")
.AssetSet("default").Is("jquery,underscore,backbone");
@mikeobrien
mikeobrien / amd-modules.js
Created February 2, 2012 20:05
Making testing of amd modules easier...
window.define = function() {
var amd = window.amd = window.amd || {};
var scripts = document.getElementsByTagName("script");
var baseUrl;
if (!amd.modules) {
for (var i = 0; i < scripts.length; i++) {
var dataMain = scripts[i].getAttribute('data-base');
if (dataMain) {
var a = document.createElement('a');
a.href = dataMain;