Skip to content

Instantly share code, notes, and snippets.

View glortho's full-sized avatar

Jed Verity glortho

View GitHub Profile
import React from 'react';
import { Api } from 'jetset';
function create( users ) {
users
.create({ name: 'foo' })
.then( data => console.log( 'Successfully created', data ) );
}
function Users({ users }) {
@glortho
glortho / dispatch_decorator_debug.js
Created December 29, 2015 19:22
Output useful debug info from Flux dispatch decorator
const groupStyle = 'font-weight: normal; color: gray';
const stackStyle = 'color: gray';
function dispatchDecoratorFactory( ACTION ) {
return function dispatchDecorator( target, key, descriptor ) {
const fn = descriptor.value;
descriptor.value = function decoratedMethod( ...args ) {
if ( myModeFlag === 'debug' ) {
console.groupCollapsed( `%c${target.constructor.name}.${key}(%O) -> ${ACTION}`, groupStyle, args );
console.debug( `%c${( new Error() ).stack}`, stackStyle );
@glortho
glortho / view_controller_listen_to.js
Created October 15, 2015 15:36
Use listenTo decorator to bind React view-controllers to stores in Flux pattern
// Note: See one implementation of this here: https://gist.github.com/jedverity/458bc65fee43fb9873e1
@listenTo( MyStore )
class MyViewController extends React.Component {
render() {
const { fromMyStore } = this.props.MyStore;
}
}
@glortho
glortho / listen_to.js
Created October 15, 2015 15:33
Decorator/higher-order React component for binding view-controllers to stores in Flux pattern
import React from 'react';
import autobind from 'autobind-decorator';
// Note: Assumes stores have a unique `key` prop and a `getState()` method
function listenTo( stores = [] ) {
const storesArray = [].concat( stores );
return function listenToDecorator( WrappedComponent ) {
@glortho
glortho / dispatch.js
Last active October 15, 2015 14:59
Use decorators for dispatching actions in Flux - https://gist.github.com/jedverity/b3e3a06761daefeca110
// Note: examples below use https://gist.github.com/jedverity/b3e3a06761daefeca110
class MyActionCreator {
@dispatch('SOME-ACTION')
updateStore(data) {
return data; // this will be dispatched with 'SOME-ACTION' as the actionType
}
@dispatch('SOME-ASYNC-ACTION')
postData(data) {
return MyApi.post('/url', data );
@glortho
glortho / keydown.js
Last active October 15, 2015 14:54
Example of keydown decorator at https://github.com/jedverity/react-keydown/
class MyComponent extends React.Component {
@keydown('up')
goUp(event) {
event.preventDefault();
this.setState({ index: -1 });
}
}
// as a higher-order component
@keydown( 'up', 'down', 'shift+enter' )
@glortho
glortho / autobind.js
Created October 15, 2015 14:44
autobind
class MyClass extends React.Component {
@autobind
myMethod() {
console.log( `${this.props} works now` );
}
}
@autobind
class MyClass extends React.Component {
myMethod() {
@glortho
glortho / prop_nullable.js
Last active April 24, 2018 11:12
Nullable PropType for React
/**
* Usage:
*
* import React from 'react';
* import nullable from 'prop_nullable';
*
* let myClass = React.createClass({
* propTypes: {
* myProp: nullable( React.PropTypes.string ).isRequired,
* myOtherProp: nullable( [React.PropTypes.string, React.PropTypes.number] )
@glortho
glortho / dispatch_decorators.js
Last active August 29, 2015 14:26
Dispatch decorators for Flux
/**
* Dispatch decorators for Flux (using ES2016 via Babel stage 1)
*
* There are some nice things about this:
*
* 1. Action types are specified at design time.
* 2. Any method's return value can be dispatched by prepending the decorator, without breaking up method definition itself.
* 3. Easier testing of action creator methods by forcing return value + simpler mocking of dispatch.
* 4. Easier grokking of all actions attached to a given class.
*/