As Front-end Engineers we mostly use libraries and/or frameworks to develop and maintain complex web apps, but what there is under the hood? Do you...
For further actions, you may consider blocking this person and/or reporting abuse
Very helpful! Thanks a lot. :)
You have a typo in there:
Should be
How extend it to include array of elements, like
I have in DOM:
< input name=product[1][quantity]>
and so on...
I would say that this is mostly a Proof of concept to show a possible implementation of two-way data binding, but for an app I would advice you to use a library (e.g. React)
Thanks for the article. How would you expand on this?
Unless I am understanding it wrong, It looks like the render function updates everything that has a binding. Would you make it so that it only updates the bindings that had changes?
Yes. You are right. Here is the updated code.
const createState = (state) => {
return new Proxy(state, {
set(target, property, value) {
target[property] = value; // default set behaviour
render(property); // updates the view every time the state changes
return true;
}
});
};
const state = createState({
name: '',
title: ''
});
const render = (property) => {
document.querySelector(
[data-model=${property}]
).value = state[property];};
const listener = (event) => {
const {type, value, dataset} = event.target;
state[dataset.model] = value;
};
document.querySelector('[data-model="name"]').addEventListener('keyup', listener);
document.querySelector('[data-model="title"]').addEventListener('keyup', listener);
Helpful article, thanks 😋
Great will try this on smaller projects.
Thanks for this info!
Maybe the next one would be a two-way binding for a list.