Skip to content

Instantly share code, notes, and snippets.

@wookets
wookets / gist:1776824
Created February 9, 2012 02:40
Global ajax error catcher in coffeescript
# handle all global ajax errors (we get a response that server is down or something)
$(document).ajaxError (e, xhr, settings, exception) ->
root.showMessage("error", "Service currently unavailable.")
@wookets
wookets / gist:1776854
Created February 9, 2012 02:49
Global scope in coffeescript
# setup a 'root' var inside a coffee wrapper that is a reference to the global scope
window.App = {}
# set a global variable
App.config =
serviceUrl: ""
# set a global function
App.hideMessage = ->
$("#message").hide()
@wookets
wookets / gist:1776979
Created February 9, 2012 03:19
Get the name of the current html page
# get page name
pathArray = window.location.pathname.split('/')
page = pathArray[pathArray.length - 1].split('.html')[0]
@wookets
wookets / login.coffee
Created February 9, 2012 19:43
amplify.js request example
amplify.request
resourceId: "login"
data:
username: @username
password: @password
success: (data) ->
alert "success! " + data.sessionToken
error: (message, level) ->
@wookets
wookets / JacksonType.java
Created February 23, 2012 00:11
Jackson Groovy Generic Object Deserializer
package wookets.json;
import org.codehaus.jackson.annotate.JsonTypeInfo;
import org.codehaus.jackson.annotate.JsonTypeInfo.As;
import org.codehaus.jackson.annotate.JsonTypeInfo.Id;
@JsonTypeInfo(use=Id.NAME, include=As.PROPERTY, property="_type", defaultImpl=Object.class)
public class JacksonType {
}
@wookets
wookets / gist:2161390
Created March 22, 2012 18:34
Angularjs route event listeners
$scope.$on '$beforeRouteChange', (ngEvent, route) ->
console.log 'before: ' + route.template
$scope.$on '$afterRouteChange', (ngEvent, route) ->
console.log 'after: ' + route.template
@wookets
wookets / gist:2997608
Created June 26, 2012 18:09
mongoose reference normalization
# this gist will use the schema and the val of a property to determine if we should normalize the incoming reference
Type = mongoose.model(resource._type)
# normalize references, since this seems to be broken...
for prop, val of resource
if Type.schema.path(prop)?.options?.ref
if utils.type(val) is 'object' then resource[prop] = val._id else resource[prop] = val
else if Type.schema.path(prop)?.options?.type?[0]?.ref
newArray = []
@wookets
wookets / app.coffee
Last active December 10, 2015 13:59
Adding JSend methods to ExpressJS 3.x
express = require 'express'
jsend = require './lib/jsend'
module.exports = app = express()
app.configure () ->
app.use(express.bodyParser()) # standard express http stack
app.use(jsend) # attach jsend and error on to every response
app.use(app.router)
@wookets
wookets / app.coffee
Last active December 10, 2015 14:28
Express 3.x error handler
express = require 'express'
error_handler = require './lib/error_handler'
module.exports = app = express()
app.configure () ->
app.use(express.bodyParser()) # standard express http stack
app.use(app.router)
app.use(error_handler) # attach error handler
@wookets
wookets / app.coffee
Created January 23, 2013 17:46
Node domain error handler... Not needed, because of express error_handler...
domain = require './server/lib/domain'
app.use(domain) # this will setup a domain for us in case an error is thrown that we haven't caught, for some reason this only works up here...