Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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
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 / 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',
window.error = (message) ->
console.log message
phantom.exit 1
#window.onerror = (message) ->
# console.log message
# phantom.exit 1
Date.prototype.elapsed = -> (new Date() - @) / 1000;
<html>
<title><use content="title" /></title>
<head>
<!--
These url's are all relative to the master layout but will
not be valid for views that exist in different folders.
I'm hoping to keep the url's relative but I'm not sure if
there is a way to accomplish that in a master layout.
(function() {
var plugin = function($) {
$.dialog = function (option) {
var content = '<div class="modal hide ' + option.css + '">';
content += '<div class="modal-header">';
content += '<a class="close" data-dismiss="modal">&times;</a><h3>' + option.title + '</h3>';
content += '</div>';
content += '<div class="modal-body">' + option.body + '</div>';
content += '<div class="modal-footer">';
content += '<a class="btn btn-primary ok">' + option.button + '</a>';
@mikeobrien
mikeobrien / backbone.lazy.js
Created April 18, 2012 04:41
Lazy plugin for Backbone
(function() {
var plugin = function(Backbone) {
LazyCollection = Backbone.Collection.extend();
LazyCollection.prototype.indexQuerystring = 'index';
LazyCollection.prototype.index = 0;
LazyCollection.prototype.lastLength = -1;
LazyCollection.prototype.fetch = function(options) {
@mikeobrien
mikeobrien / bootstrap.validate.js
Created April 19, 2012 04:21
Simple validation for bootstrap horizontal forms
(function() {
var plugin = function($) {
$.fn.validate = function (selectors, predicate, message) {
if (!$.isArray(selectors)) selectors = [selectors];
var controlGroups = [];
var values = [];
for (index in selectors) {
var input = this.find(selectors[index]);
if (input.length == 0) throw 'Selector invalid: ' + selectors[index];
input = $(input[0]);