Skip to content

Instantly share code, notes, and snippets.

@gilbox
gilbox / Functional React: Phone Input Demo.js
Created June 14, 2015 16:33
Functional React: Phone Input Demo
const {fromJS} = Immutable;
const subedit = (edit, ...path) => transform =>
edit(state => state.updateIn(path, transform));
const data = fromJS({
phone: '6665552222'
});
const parsePhone = v => v.replace(/\D/g, '').substr(0,10);
@gilbox
gilbox / Functional React: component with ComponentRenderMixin staticFunctionsMixin.js
Last active August 29, 2015 14:23
Functional React: component Function with ComponentRenderMixin and staticFunctionsMixin
// ComponentRenderMixin is a slightly modified version of PureRenderMixin
const ComponentRenderMixin = require('component-render-mixin');
function delegate(delegee) {
var delegate = function() {
return delegate.delegee.apply(this, arguments);
}
delegate.delegee = delegee;
delegate.isDelegate = true;
return delegate;
@gilbox
gilbox / Functional React: Component Function with ComponentRenderMixin.js
Created June 13, 2015 09:26
Functional React: Component Function with ComponentRenderMixin
// @param additionalMixins? {Array|Object}
// @param renderFn {Function}
// @returns Component
function component(additionalMixins, renderFn) {
renderFn = renderFn || additionalMixins;
additionalMixins = (additionalMixins instanceof Function) ? [] : [].concat(additionalMixins);
const mixins = [ComponentRenderMixin].concat(additionalMixins);
const render = function() {
@gilbox
gilbox / Functional React: Encapsulated State Changes.js
Last active August 29, 2015 14:23
Functional React: Encapsulated State Changes
const {fromJS} = require('immutable');
const data = fromJS({ name: 'gilbox' });
const Hello = component(function Hello ({name}, {edit}) {
return <div>
Hello, {name}!
<button onClick={event => edit(name => 'Maude Lebowski') }>
Change My Name
</button>
</div>
@gilbox
gilbox / Functional React: Rudimentary State Changes.js
Last active August 29, 2015 14:23
Functional React: Rudimentary State Change
const {fromJS} = require('immutable');
const data = fromJS({ name: 'gilbox' });
const Hello = component(function Hello ({name}, {edit}) {
return <div>
Hello, {name}!
<button onClick={event => edit(data => data.updateIn(['name'], 'Maude Lebowski') }>
Change My Name
</button>
</div>
@gilbox
gilbox / Functional React: subedit.js
Created June 13, 2015 08:57
Functional React: subedit
const subedit = (edit, ...path) => transform =>
edit(state => state.updateIn(path, transform));
@gilbox
gilbox / Functional React: The Renderer Component.js
Last active August 29, 2015 14:23
Functional React: The Renderer Component
const {fromJS} = require('immutable');
const data = fromJS({ name: 'gilbox' });
const rendererMixin = {
getInitialState() {
return {data:this.props.data}
},
edit (transform) {
this.setState({data: transform(this.state.data)})
}
@gilbox
gilbox / gist:a7ff39cb75796c4413cb
Created March 27, 2015 16:08
Use transit-js with Immutable.js and plain JS Objects mixed in
var Transit = require('transit-js'),
Imm = require('immutable');
// gil: It's possible that the following reader/writer configuration is incomplete.
// This is a modified version of:
// https://gist.github.com/Tvaroh/52efbe8f4541ca537908
var reader = Transit.reader('json', {
mapBuilder: {
init: function () { return {}; },
@gilbox
gilbox / transit-immutable.js
Created February 13, 2015 00:16
Use transit-js Immutable.js with automatic conversion of plain JS objects to immutables
var Transit = require('transit-js'),
Imm = require('immutable');
// gil: It's possible that the following reader/writer configuration is incomplete.
// This is a modified version of:
// https://gist.github.com/Tvaroh/52efbe8f4541ca537908
//
// ... the original version of this utilized built-in JS (ES6?) types for
// better efficiency+speed. However, it didn't work with nested Map + OrderedMap.
// Improved efficiency+speed can be achieved by re-implementing with some native types.
@gilbox
gilbox / gist:12095aa0bc6822736f8f
Last active September 1, 2015 21:49
ui-router VS flux+vanillaJS
//
// ui-router
//
$stateProvider.state('person', {
url: '/person/{personId}',
resolve: ($stateParams, AppObject) => {
return AppObject.getPerson( $stateParams.personId )
},
controller() { /* ... */ }