Skip to content

Instantly share code, notes, and snippets.

@xfsnowind
xfsnowind / parent->child-triggered-child.js
Created October 23, 2015 22:41
communication from parent to child and triggered by child on blog "React -- Communication between components" on 2014/06/24
// For Parent.js
var Parent = React.createClass({
getInitialState: function () {
return { parentValue: "" };
},
// the callback function is passed to Child as props
passValueFunc: function (para) {
if (this.state[para] != undefined) {
return this.state[para];
@xfsnowind
xfsnowind / child->parent-trigged-parent.js
Last active October 23, 2015 22:41
communication from child to parent and triggered by parent on blog "React -- Communication between components" on 2014/06/24
// For Parent.js
var Parent = React.createClass({
getInitialState: function () {
return { childRef: "childRef" };
},
changeHandler: function () {
// use ref to call the instance of Child
this.refs[this.state.childRef].getChildValue();
},
@xfsnowind
xfsnowind / parent->child-triggered-parent.js
Last active October 23, 2015 22:40
communication from parent to child and triggered by parenton blog "React -- Communication between components" on 2014/06/24
// For Parent.js
var Parent = React.createClass({
getInitialState: function () {
return {
changedValue: ""
};
},
// the callback function for changing the text in <input type="text"></input>
changeHandler: function (event) {
@xfsnowind
xfsnowind / prepare.js
Created October 23, 2015 22:36
preparation codes on blog "React -- Communication between components" on 2014/06/24
// For Parent.js
var Parent = React.createClass({
render: function () {
return (
<div><input onchange="" type="text" value="Parent"></input><child></child></div>
);
}
});
// For Child.js
@xfsnowind
xfsnowind / debounce.js
Created October 23, 2015 22:31
debounce method on blog "Introducing Javascript method debounce" on 2014/05/12
function debounce(func, wait, immediate) {
var timeout;
return function () {
var obj = this, argus = arguments;
function delayed () {
if (!immediate) {
func.apply(obj, argus);
}
timeout = null;
}
@xfsnowind
xfsnowind / debounce-resize--with-immediate.js
Created October 23, 2015 22:28
apply debounce with parameter immediate to resize event on blog "Introducing Javascript method debounce" on 2014/05/12
var timeout;
var immediate = true;
var wait = 500;
$(window).resize(function () {
console.log("debounced");
function delayed () {
if (!immediate) {
//the operation needed
}
timeout = null;
@xfsnowind
xfsnowind / debounce-resize.js
Created October 23, 2015 22:25
apply debounce to resize event on blog "Introducing Javascript method debounce" on 2014/05/12
var timeout;
$(window).resize(function () {
function delayed () {
//the operation needed
timeout = null;
}
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(delayed, 500);
@xfsnowind
xfsnowind / debounce.clj
Created October 23, 2015 22:08
The clojure version with library core.async of debounce function
(defn debounce-chan
"Taken from https://github.com/swannodette/async-tests
A little different with original one, write to channel after the interval
instead of doing it in the beginning"
([source msecs]
(debounce-chan (chan) source msecs))
([c source msecs]
(go-loop [state ::init
last-one nil
cs [source]]
@xfsnowind
xfsnowind / castle-lazy-array.js
Created September 29, 2015 19:10
lazy operations to html node array on blog "Handle Jquery object with Lazy.js" on 2014/04/07
lazy($(".castle").toArray()).map(mapFunc)
.reduce(reduceFunc)
.each(eachFunc);
@xfsnowind
xfsnowind / castle-lazy-length.js
Created September 29, 2015 19:09
length of lazy operations to html nodes on blog "Handle Jquery object with Lazy.js" on 2014/04/07
//show the length of $(".castle")
> lazy($(".castle")).size()
154
//show the length of functions in $(".castle")
> lazy($(".castle")).functions().size()
145