This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function differenceFromSecondArray(arr1, arr2) { | |
let arr1Map = arr1.reduce((acc, curr) => { | |
return { ...acc, [curr]: 1 } | |
}, {}) | |
return arr2.reduce((acc, curr) => arr1Map.hasOwnProperty(curr) ? acc : acc.concat(curr), []) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let reducer = function(state = 0, action) { | |
switch(action.type) { | |
case 'INCREMENT': | |
return state + 1; | |
case 'DECEMENT': | |
return state - 1; | |
default: | |
return state; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let totalCount = 0; | |
let render = count => document.getElementById('count').innerHTML = count; | |
document.getElementById('increment') | |
.addEventListener('click', () => { | |
totalCount += 1; | |
render(totalCount); | |
}); | |
document.getElementById('decerement') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let counter = new ReactiveHbs({ | |
container: '.mount', // html template mount point | |
template: '#tpl', // hadnlebars template | |
data: { | |
count: 0 | |
} | |
}); | |
// Events | |
counter.events({ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ReactiveHbs.promises({ | |
getAllUsers() { | |
return $.get('https://api.github.com/users'); | |
}, | |
}); | |
// usage | |
ReactiveHbs.executePromise('getAllUsers', (err, data) => { | |
if ( !err ) console.log(data); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// non-delegated events | |
ReactiveHbs.onRendered(function() { | |
let self = this; | |
$('button[type="submit"]').on('click', (e) => { | |
e.preventDefault(); | |
self.set('someData', $(elem).attr('data-text')); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// bind delegated events | |
ReactiveHbs.events({ | |
'click button[type="submit"]': (e, elm, tpl) => { | |
e.preventDefault(); | |
tpl.set('someData', $(elem).attr('data-text')); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// bind helpers to only one template | |
ReactiveHbs.helpers({ | |
add(x, y) { | |
return x + y; | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// updates the DOM as well | |
ReactiveHbs.set('counter', counter.get() + 1); | |
// Observer | |
ReactiveHbs.reactOnChange('counter', { debounce: 1000 }, (tpl) => { | |
console.log( 'counter have been changed ' ); | |
console.log( 'value', tpl.get('counter') ); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('div[name="some"]').on('click', () => { | |
console.log('div have been clicked !'); | |
}); |