Skip to content

Instantly share code, notes, and snippets.

View choonkending's full-sized avatar

Ken Ding choonkending

View GitHub Profile
{
"listing": {
"features": {
"bedrooms": {
"value": 3
}
}
}
}
@choonkending
choonkending / printTiming.js
Last active January 15, 2017 23:15
Print navigation timing in your browser
const events = [ 'domLoading', 'domInteractive', 'domContentLoadedEventEnd', 'domComplete', 'loadEventEnd' ];
const startEvent = 'navigationStart';
const timing = performance.timing;
const compose = (f, g) => x => f(g(x));
const timeFromNavigationStart = event => differenceInSeconds(timing[event], timing[startEvent]);
const differenceInSeconds = (t1, t2) => (t1 - t2) / 1000;
const sanitizeTime = t => (t > 0) ? t : 'N/A - Has not fired yet - please run console log again';
const createEventHandlers = (el = document, eventName, handler) => ({
add: () => { el.addEventListener(eventName, handler); },
remove: () => { el.removeEventListener(eventName, handler); }
});
/*
* In component
*/
/* Modal */
<div className="modal">
<CloseButton />
</div>
/* CloseButton */
<button className="close-button">Close</button>
@choonkending
choonkending / separationOfConcerns.js
Created September 21, 2016 08:55
An example of the evolution of a component
/* Start with building the component */
import React from 'react';
class Value extends React.Component {
constructor(props) {
super(props);
this.state = { isActive: false };
this.onToggle = this.onToggle.bind(this);
}
/*
* Backbone view
*/
var View = Backbone.View.extend({
modifyDOM: function() {
this.$el.html(this.template(obj));
},
render: function() {
this.$el.html(this.template(obj));
// https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch2.md
/*
* Called with new? Use the newly constructed object
*/
class Pokemon {
constructor(hp, mp) {
this.hp = hp;
this.mp = mp;
}
}
@choonkending
choonkending / thisChanges.js
Created September 21, 2016 05:05
how this used to be used
button.addEventListener('click', function(e) {
// Something
}.bind(this));
button.addEventListener('click', e => {
// Something
});
class ViewModel {
constructor(prop1, prop2) {
/*
* Comparing arrow functions' lexical this binding to ordinary functions
*/
function Dog() {
// The arrow function binds this to the new Dog instance
// It is the same as writing it as
// const doSomething = function() { this.bark(); }.bind(this);
const doSomething = () => { this.bark() };
doSomething();
}
import React, { Component, PropTypes } from 'react';
export default class Toggle extends Component {
constructor(props) {
super(props);
this.state = { isActive: false };
this.onToggle = this.onToggle.bind(this);
}
render() {