Skip to content

Instantly share code, notes, and snippets.

@amoe
Created November 24, 2017 14:34
Show Gist options
  • Save amoe/2130dde546fa487c2875ccf011243dc9 to your computer and use it in GitHub Desktop.
Save amoe/2130dde546fa487c2875ccf011243dc9 to your computer and use it in GitHub Desktop.
one way binding in vanilla js
// proxies.js
const initialState = {
count: 0
};
const configuration = {
count: '#my-textarea'
};
const state = makeReactive(initialState, configuration);
function makeReactive(state, configuration) {
handler = {
'set': function(target, property, value, receiver) {
console.log("setting property");
const selector = configuration[property];
const domTarget = document.querySelector(selector);
domTarget.textContent = value;
target[property] = value;
return true;
}
};
var proxy = new Proxy(state, handler);
return proxy;
}
function increment(event) {
state.count += 1;
}
function wireInterface(event) {
const button = document.querySelector('#increment');
button.addEventListener('click', increment);
}
document.addEventListener('DOMContentLoaded', wireInterface);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment