Skip to content

Instantly share code, notes, and snippets.

@bendrucker
bendrucker / component.js
Created February 29, 2016 17:58
Async component transitions
Depots.load = function load (state, callback) {
xhr('...', function (err, data) {
if (err) return callback(err)
state[arrayKey].set(data)
callback(null, data)
})
}
// app.js
@bendrucker
bendrucker / reducer.js
Created February 16, 2016 00:07
Using reducers to avoid hard-to-read side effects. This is probably a bad idea unless your list is really long and rarely changes. You should probably just loop multiple times and then cache the result efficiently using an immutable store.
return orders.reduce(function (accumlator, order) {
accumulator.aggregates.count++
accumulator.aggregates.total += order.total
accumulator.orders.push(order)
return accumulator
}, OrderStore())
function OrderStore () {
return {
@bendrucker
bendrucker / controller.js
Created February 12, 2016 04:04
Sane Angular controllers, minus the popular anti-patterns
function DeliveryController (injectables...) {
var controller = {
drivers: drivers,
fetch: fetch
}
return controller
function fetch () {
return $http.get('drivers')
@bendrucker
bendrucker / module.js
Created February 9, 2016 16:21
Organizing JS modules using keywords and language behavior
var dep = require('dep')
module.exports = {
myFn: myFn
}
function myFn (input) {
var myVar = {}
// ...
function getUrls (callback) {
fs.readFile('./cache.json', function (err, data) {
if (err && err.code !== 'ENOENT') return callback(err)
getDataFromServer(function (err, data) {
if (err) return callback(err)
callback(null, data)
fs.writeFile('./cache.json', JSON.stringify(data), noop)
})
})
}
Verifying that +bendrucker is my blockchain ID. https://onename.com/bendrucker
@bendrucker
bendrucker / router.js
Last active September 22, 2015 15:14
Playing with routing ideas
Router.hook(route.profile, function (state) {
state.put('profile', Profile())
})
Router.unhook(route.profile, function (state) {
state.delete('profile')
})
Router.hook(route.order, function (state, params, callback) {
var order = Order({id: params.id})
@bendrucker
bendrucker / login.js
Created September 13, 2015 01:56
Here's how I'd build a simple form
var Struct = require('observ-struct')
var Email = require('email-input')
var Password = require('password-input') // doesn't actually exist yet
var value = require('observ-value')
var h = require('virtual-dom/h')
var when = require('value-when')
function LoginForm (data) {
data = data || {}
@bendrucker
bendrucker / droplr-gh.md
Last active February 1, 2021 12:30
Pasting droplr image/gif links to Github

Droplr will auto-copy the sharing link when you take a screenshot. You'll use this on GitHub.

That copied link will look like this: http://d.pr/i/13PDt

To paste that into a GitHub issue:

![](http://d.pr/i/13PDt+)
@bendrucker
bendrucker / styling-diffable-modular-uis.md
Created August 15, 2015 05:04
Styling diffable/modular UIs

Styling Diffable UIs

Presentation is an important part of a UI component. Diffing makes it signifcantly easier to build well-encapsulated components, but styles still pose problems. CSS is global and hard to modularize. For simple cases, inline styles fix the problem easily:

var h = require('virtual-dom/h')

function render (state) {
	var style = {
 color: state.enabled ? 'red' : undefined