Skip to content

Instantly share code, notes, and snippets.

@Rich-Harris
Rich-Harris / ractive-ajax.md
Created November 11, 2014 23:56
Ractive + AJAX

Since Ractive is solely concerned with rendering UI (like React), it doesn't have any opinions about where its data comes from and goes to. In theory this makes it ultra-flexible (want to use WebSockets? Fine. Want to use the Backbone adaptor? Also fine) but in practice we could probably do a much better job of coming up with some decent patterns and creating tutorials out of them, since this is a question that comes up often.

It's totally possible to just use jQuery (or AJAX lib of your choice), and do this sort of thing:

var updating;

getJSON(endpoint + '/' + id + '.json').then(function (data) {
  if (updating) return; // prevent infinite loops!
  
@Rich-Harris
Rich-Harris / BaseView.html
Last active August 29, 2015 14:18
Backbone adaptor
<div class='info'>
{{#selectedFilm}}
<!-- roll over the actor/director name to reveal credits -->
<div class='credits'>
{{#actor}}
<div class='actor' on-hover='highlight:{{this}}'>
<span>{{name}}</span>
{{#highlighted}}
@Rich-Harris
Rich-Harris / Range.js
Last active August 29, 2015 14:24
math.js ES6 example
import { format, isNumber, sign } from 'number';
import { isString } from 'string';
/**
* @constructor Range
* Create a range. A range has a start, step, and end, and contains functions
* to iterate over the range.
*
* A range can be constructed as:
* var range = new Range(start, end);
@Rich-Harris
Rich-Harris / get.js
Last active September 23, 2015 13:57
XHR demo
function get ( url ) {
return new Promise( function ( fulfil, reject ) {
var xhr = new XMLHttpRequest();
xhr.open( 'GET', 'my-data.json' );
xhr.onload = function () {
fulfil( xhr.responseText );
};
@Rich-Harris
Rich-Harris / App.html
Created September 23, 2015 14:24
ractive-load demo 2
<h1>hello {{name}}!</h1>
<style>
h1 { color: red }
</style>
@Rich-Harris
Rich-Harris / App.html
Last active September 23, 2015 14:24
ractive-load demo 1
<h1>hello {{name}}!</h1>
<style>
h1 { color: red; }
</style>
<script>
component.exports = {
oninit: function () {
get( 'data.json' )
@Rich-Harris
Rich-Harris / .eslintrc
Created September 26, 2015 12:55
My eslintrc
{
"rules": {
"indent": [ 2, "tab", { "SwitchCase": 1 }],
"quotes": [ 2, "single" ],
"linebreak-style": [ 2, "unix" ],
"semi": [ 2, "always" ],
"no-mixed-spaces-and-tabs": [ 2, "smart-tabs" ],
"object-shorthand": [2, "always" ],
"no-const-assign": 2,
"no-class-assign": 2,
@Rich-Harris
Rich-Harris / redux.js
Last active October 27, 2015 22:51
Redux generated with Webpack
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["Redux"] = factory();
else
root["Redux"] = factory();
})(this, function() {
@Rich-Harris
Rich-Harris / redux.es6.js
Created November 17, 2015 00:12
Redux as a distributable ES6 module
var babelHelpers = {};
babelHelpers._extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
@Rich-Harris
Rich-Harris / addCommaSeparators.js
Created June 11, 2013 12:05
Add comma separators (e.g. `addCommaSeparators(1234567) === '1,234,567'`)
(function ( global ) {
'use strict';
var addCommaSeparators = function ( num, precision ) {
var integer, decimal, remaining, result, lastThree;
integer = Math.floor( num );
decimal = num - integer;