Skip to content

Instantly share code, notes, and snippets.

// In JSX, callback can get passed directly as part of api
<MyComponent onSomething={handler} />
// With events, there is a disconnect between the event publisher and the consumer of the event
<my-component></my-component>
// Some parent, but could be multiple, hard to follow
let myComponent = this.querySelector('my-component');
myComponent.addEventListener('something', handler);
@gbabiars
gbabiars / ghost-to-gatsby.js
Created May 20, 2017 01:10
A script to migrate posts from Ghost's exported json to Gatsby
const fs = require('fs');
const path = require('path');
const moment = require('moment');
const json = require('./greg-babiarss-blog.ghost.2017-05-13.json');
json.data.posts
.filter(p => p.status === 'published')
.forEach(({title, slug, published_at: published, markdown}) => {
const date = moment(published).format('YYYY-MM-DD');
const content =
const createStore = (reducer, initialState) => {
let dispatcher$ = new Rx.Subject();
let state$ = new Rx.BehaviorSubject(initialState);
dispatcher$.scan(reducer, state$.getValue()).subscribe(state$);
return {
dispatch(action) {
dispatcher$.next(action);
},
@gbabiars
gbabiars / Scores.js
Created December 20, 2015 21:15
Basic example of RxJS and React
import React from 'react';
import axios from 'axios';
import Rx from 'rxjs';
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
scores: []
};
@gbabiars
gbabiars / Ember Data Camelcase serializer
Created August 11, 2013 04:21
An example of the overrides needed to use camel case routes and json.
App.Serializer = DS.RESTSerializer.extend({
keyForAttributeName: function(type, name) {
return name;
},
keyForBelongsTo: function(type, name) {
var key = this.keyForAttributeName(type, name);
if (this.embeddedType(type, name)) {
return key;
@gbabiars
gbabiars / Grunt Proxy Server
Last active September 6, 2016 05:45
Grunt Connect Server with Proxies
var proxySnippet = require('grunt-connect-proxy/lib/utils').proxyRequest;
module.exports = function(grunt) {
grunt.initConfig({
watch: {
all: {
files: ['Gruntfile.js']
}
},
connect: {
@gbabiars
gbabiars / Qunit Sinon cache test
Created July 11, 2013 14:20
An example of how to unit test caching within a function that only makes one
test('calling myFunc twice with the same url should only make one request to that url', function() {
var fakeServer = sinon.fakeServer.create(), // initialize fake server
myModule = myModule(), // whatever you need to do to instantiate your system under test
url = '/user/12',
callback = function() {}; // does nothing
fakeServer.autoRespond = true; // make sure the fake response is sent after every request
fakeServer.respondWith('GET', url,
[200, { 'Content-Type': 'application/json' }, '[{ "id": 12, "name": "John Doe" }]']); // define your fake response that the url will send
@gbabiars
gbabiars / Extend With Mixin
Last active December 19, 2015 09:48
Examples of how to create Backbone view classes with mixins.
var mixin1, mixin2, mixinArray;
mixin1 = {
something: function() {
return 'something';
}
};
mixin2 = {
another: function() {
.board { width: 500px; height: 500px; background-color: #ff6a00 }
}