Skip to content

Instantly share code, notes, and snippets.

View nightspirit's full-sized avatar

Pochen Lin nightspirit

  • Everbridge
  • United States
View GitHub Profile
@nightspirit
nightspirit / componentWillUpdate.js
Created April 11, 2018 08:27
React 16.3 lifecycle hook
// before
componentWillUpdate(nextProps, nextState) {
if (
this.state.someStatefulValue !==
nextState.someStatefulValue
) {
nextProps.onChange(nextState.someStatefulValue);
}
}
@nightspirit
nightspirit / fetch-external-data.js
Last active April 11, 2018 08:26
React 16.3 Life cycle hooks example
//before
componentWillMount() {
this._asyncRequest = asyncLoadData()
.then(externalData => {
this._asyncRequest = null
this.setState({externalData})
})
}
//after
@nightspirit
nightspirit / observable.js
Last active October 5, 2017 21:24
Observable
function Observable(init_value) {
var _value = init_value;
var subscriber = [];
function obs() {
if (arguments.length) {
_value = arguments[0];
obs.trigger();
return this;
} else {
@nightspirit
nightspirit / defer.js
Created March 20, 2015 23:12
Javascript Defer snippet
function Defer () {
var result = {};
result.promise = new Promise(function(resolve, reject) {
result.resolve = resolve;
result.reject = reject;
});
return result;
};
@nightspirit
nightspirit / cookie.js
Last active August 29, 2015 14:14
Global cookie getter/setter
@nightspirit
nightspirit / ngUpload.htm
Last active August 29, 2015 14:14
Angular directive for blueimp/jQuery-File-Upload
<div ng-controller="uploadCtrl as upload">
<!-- input file
input name can be changed to fit the server side
-->
<input type="file" name="myFile"
file-read
upload-data="upload.data",
preview="upload.preview",
validate="upload.validate.file"/>