Skip to content

Instantly share code, notes, and snippets.

Avatar

Joshua Comeau joshwcomeau

View GitHub Profile
@joshwcomeau
joshwcomeau / gist:49dc6a7e7246437dd09e
Last active Aug 29, 2015
Chrome Extension Resources
View gist:49dc6a7e7246437dd09e
@joshwcomeau
joshwcomeau / AnimalAndDog.js
Last active Feb 2, 2016
An example of concatenative inheritance
View AnimalAndDog.js
var animal = {
position: [0,0],
species: "mammal",
move: function(distance, axis) {
axis === 'x'
? this.position[0] += distance
: this.position[1] += distance;
}
}
@joshwcomeau
joshwcomeau / DogOutput.js
Last active Aug 29, 2015
Concatenative Inheritance merge output
View DogOutput.js
console.log(dog);
// would return:
{
position: [0,0],
move: function(distance, axis) { ... },
species: "canine",
bark: function() { ... }
}
View ApplicationController.js
window.reqKitControllers.application = {
// Fetch an array of 'Emotion' objects from the server.
// Returns a promise.
fetchEmotions: function() {
return $.ajax(window.reqKitConstants.ApiEmotionIndex, {
method: 'GET',
contentType: "application/json",
headers: {
'Authorization': window.reqKitConstants.ApiKey
}
View CatIndexController.js
// Cat Index Controller
window.reqKitControllers.catIndex = _.extend({}, window.reqKitControllers.application, {
emotionSelector: "#select-emotion",
initialize: function() {
this.populateEmotionsSelect(this.emotionSelector, " ", true);
}
});
// Cat New Controller
@joshwcomeau
joshwcomeau / RequestKitten.js
Last active Aug 29, 2015
Requesting Kittens like mad!
View RequestKitten.js
$.ajax("http://requestkittens.com/cats", {
data: {
emotion: "grumpy",
imageSize: "full",
numOfResults: 10
},
headers: {
'Authorization': <your API key here>
}
});
View CatsIndexResponse.json
{
"_items": [
{
"id": "553fff884c7f7f44fa6b7fe7",
"url": "https://s3.amazonaws.com/requestkittens/553c0437e4b0bde7020ac956-b469c2c29d887b46d9019839d9a70df4/full.jpg",
"emotion": "happy",
"source": {
"owner": "we-lovecats.tumblr.com",
"url": "http://40.media.tumblr.com/bdebb1aaa241e3faeb70233145c0cd87/tumblr_n6t7upX1LR1rouu9to1_400.jpg"
}
View EmotionsIndexResponse.json
{
"_items": [
{
"_id": "553ba848e4b0bde7020ac65d",
"name": "sad"
},
{
"_id": "553ba851e4b0bde7020ac65e",
"name": "happy"
},
View CatsShowResponse.json
{
"_id": "553d327e594a1b5f1b13f198",
"url": {
"thumb": "https://s3.amazonaws.com/requestkittens/553c0437e4b0bde7020ac956-d9ed2ad384499394dde0eaa8c0ed040a/thumb.jpg",
"small": "https://s3.amazonaws.com/requestkittens/553c0437e4b0bde7020ac956-d9ed2ad384499394dde0eaa8c0ed040a/small.jpg",
"medium": "https://s3.amazonaws.com/requestkittens/553c0437e4b0bde7020ac956-d9ed2ad384499394dde0eaa8c0ed040a/medium.jpg",
"full": "https://s3.amazonaws.com/requestkittens/553c0437e4b0bde7020ac956-d9ed2ad384499394dde0eaa8c0ed040a/full.jpg"
},
"emotion": [
{
@joshwcomeau
joshwcomeau / redux_react.js
Created Dec 11, 2015
Really basic Redux + React example
View redux_react.js
"use strict";
const connect = ReactRedux.connect;
const Provider = ReactRedux.Provider;
// REDUCER
const defaultState = { count: 0 };
const appReducer = (state = defaultState, action) => {
switch ( action.type ) {
case 'increment':
return _.extend({}, state, { count: state.count+1 });