Skip to content

Instantly share code, notes, and snippets.

@dylants
dylants / authenticated-model.js
Created January 7, 2014 17:39
A Backbone Model which provides a hook to handle unauthorized API requests to the server. When dealing with server side security, this is one way of handling an unauthorized user on the client side in a generic way. By extending this model rather than Backbone's model, you can decide what should happen to the user when they hit an unauthorized r…
/* global define:true */
define([
"backbone"
], function(Backbone) {
"use strict";
// Extend Backbone's Model to override the sync function. Doing so allows us
// to get a hook into how the errors are handled. Here we can check if the
// response code is unauthorized, and if so, navigate to the login page
return Backbone.Model.extend({
@dylants
dylants / app.js
Last active December 23, 2015 22:49
Node Express app.js (generic)
var express = require("express"),
fs = require("fs"),
cons = require("consolidate"),
app = express();
// configure the app (all environments)
app.configure(function() {
// read the port from the environment, else set to 3000
app.set("port", process.env.PORT || 3000);
@dylants
dylants / java_version_control
Created August 26, 2013 19:52
Determine which version of Java you have running / Change the version of Java (these directions are for a Mac)
To list the Java versions installed on your machine (note the big V):
/usr/libexec/java_home -V
To change the version of Java (note the small v):
export JAVA_HOME=`/usr/libexec/java_home -v 1.7`
export JAVA_HOME=`/usr/libexec/java_home -v 1.6`
The default JAVA_HOME is:
export JAVA_HOME=/Library/Java/Home
@dylants
dylants / context.xml
Created August 22, 2013 20:56
Context.xml to be defined within a Java web application, under src/main/webapp/META-INF. This defines the database connection using JDBC within Tomcat. Defining this within the context.xml will configure the container, and allow the Java web app to connect via JNDI. This can also be included in the Tomcat server.xml, keeping configuration outsid…
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE project>
<Context>
<Resource name="jdbc/web"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
testWhileIdle="true"
testOnBorrow="true"
testOnReturn="false"
@dylants
dylants / backboneValidationSetup.js
Created August 15, 2013 18:42
Backbone Validation setup (with Backbone 1.0, backbone.validation plugin, and bootstrap integration)
/*
* MODEL
*/
return Backbone.Model.extend({
urlRoot: "/api/resource",
defaults: {
id: null,
name: null,
num: null,
@dylants
dylants / modelAndSerializeCode.js
Last active December 21, 2015 03:38
Backbone form submission, serialize the form to set in model (form field names must match model variable names)
/*
* MODEL CODE
*/
formSubmit: function(ev) {
var formValues;
ev.preventDefault();
formValues = this.$("form").serializeObject();
this.model.save(formValues);
@dylants
dylants / code.js
Created August 13, 2013 22:44
Upload an image from a Backbone/jQuery client side app to a Node server
/*
* CLIENT SIDE CODE
*/
// perform a file upload
file = this.$("input[name='image-file']").prop("files")[0];
formData = new FormData();
formData.append("image", file);
$.ajax({
@dylants
dylants / app.js
Created July 30, 2013 16:43
simple node proxy using http-proxy
var app, routingProxy, apiProxy, proxyHost, proxyPort;
var express = require("express"),
httpProxy = require("http-proxy"),
fs = require("fs");
// Should we dynamically define these by environment?
proxyHost = "localhost";
proxyPort = "8080";
// Create and define the Express server
@dylants
dylants / Gruntfile.js
Created July 18, 2013 19:28
Gruntfile for mocha/jenkins integration
module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks("grunt-mocha-test");
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
clean: ["test-reports.xml"],
mochaTest: {
"test": {
@dylants
dylants / view.js
Created July 17, 2013 15:41
Backbone sync success and failure
return Backbone.View.extend({
...
events: {
"click #submit-button": "submit"
},
initialize: function() {
this.model.on( "sync", this.syncSuccess, this );