Skip to content

Instantly share code, notes, and snippets.

Avatar

Pochen Lin nightspirit

  • Everbridge
  • United States
View GitHub Profile
@nightspirit
nightspirit / componentWillUpdate.js
Created Apr 11, 2018
React 16.3 lifecycle hook
View componentWillUpdate.js
// before
componentWillUpdate(nextProps, nextState) {
if (
this.state.someStatefulValue !==
nextState.someStatefulValue
) {
nextProps.onChange(nextState.someStatefulValue);
}
}
@nightspirit
nightspirit / fetch-external-data.js
Last active Apr 11, 2018
React 16.3 Life cycle hooks example
View fetch-external-data.js
//before
componentWillMount() {
this._asyncRequest = asyncLoadData()
.then(externalData => {
this._asyncRequest = null
this.setState({externalData})
})
}
//after
View observable.js
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 Mar 20, 2015
Javascript Defer snippet
View defer.js
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 Aug 29, 2015
Global cookie getter/setter
View cookie.js
@nightspirit
nightspirit / ngUpload.htm
Last active Aug 29, 2015
Angular directive for blueimp/jQuery-File-Upload
View ngUpload.htm
<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"/>