This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>JS Design Patterns</title> | |
<meta charset="utf-8" /> | |
</head> | |
<body> | |
<h1>Singleton Pattern</h1> | |
<button id="createWorld">Create a World</button> <button id="createAnotherWorld">Create Another World</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>JS Design Patterns</title> | |
<meta charset="utf-8" /> | |
</head> | |
<body> | |
<h1>Factory Pattern</h1> | |
<button id="buildCar">Build a car</button> <button id="buildMotorbike">Build a motorbike</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Vehicle(manufacturer) { | |
this.manufacturer = manufacturer; | |
} | |
Vehicle.prototype.sayHello = function () { | |
alert('Hi there'); | |
}; | |
function Unicycle(manufacturer, spokes) { | |
Vehicle.call(this, manufacturer); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a table and attach to the target div | |
myTable = $('<table><caption><thead><tbody>').appendTo('#myDiv'); | |
// Loop through object and put the keys/values in table cells on the page | |
Object.keys(obj).forEach(function(key) { | |
if (obj[key] !== null) { | |
currentRow = $('<tr>'); | |
cell1 = $('<td>').text(key).appendTo(currentRow); | |
cell2 = $('<td>').text(obj[key]).appendTo(currentRow); | |
currentRow.appendTo('tbody'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function initLocalStorageControls() { | |
var userMessage, dataKey, dataValue, storedData, dataObj = {}, dataString = '', obj = {}; | |
if (Modernizr.localstorage) { | |
console.log('Local storage is available'); | |
$('form').fadeIn(); | |
userMessage = 'Use the form below to add and display data.'; | |
// Pre-fill form | |
storedData = localStorage.getItem('userData'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
console.log('Document ready'); | |
fetchEmployeeData(); | |
function fetchEmployeeData() { | |
console.log('Running fetchEmployeeData()'); | |
// Get JSON | |
$.ajax({ | |
type: 'GET', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import the twit module that was downloaded with npm install twit. | |
// The module is loaded in node_modules and Node.js knows where to find it. | |
var Twit = require('twit'); | |
// Import the MongoDB connection stuff | |
var mongoose = require('mongoose'); | |
// Connect to the database - this also creates the database if it doesn't exist | |
mongoose.connect('mongodb://localhost/twittersearch'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import the twit module that was downloaded with npm install twit. | |
// The module is loaded in node_modules and Node.js knows where to find it. | |
var Twit = require('twit'); | |
// Settings -- put your keys here: | |
var T = new Twit({ | |
consumer_key: '...', | |
consumer_secret: '...', | |
access_token: '...', | |
access_token_secret: '...' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app.js | |
var express = require('express') | |
, routes = require('./routes') | |
, user = require('./routes/user') | |
, http = require('http') | |
, path = require('path') | |
// Include another file | |
, score = require('./routes/score'); | |
app.get('/', routes.index); |
NewerOlder