by tjallingt · 10y ago · 0 comments
children(el => [
el.email = input(props({ type: 'email' })),
el.pass = input(props({ type: 'pass' })),
el.submit = button(text('Sign in'))
])
Can someone explains what this does and why it is used here?
As i understand it this function both modifies the `el` object (whatever that is) and returns an array containing the input elements, but why would you want to do both those things at the same time? const login = document.createElement('form');
login.email = document.createElement('input');
login.email.type = 'email';
login.pass = document.createElement('input');
login.pass.type = 'pass';
login.submit = document.createElement('button');
login.submit.textContent = 'Sign in';
login.appendChild(login.email);
login.appendChild(login.pass);
login.appendChild(login.submit);
login.onsubmit = function(event) {
event.preventDefault();
console.log(this.email.value, this.pass.value);
};
document.body.appendChild(login);
The children call produces an array of objects that are appended to the element, but the callback also takes the wrapping element (in this case `login`), and so to be conveniently able to access it later (`login.email.value`) it also assigns them there. A more conventional JavaScript approach would be to assign email, pass and submit as local variables rather than stuffing them inside the form object.As for the other example:
const app = document.createElement('table');
const tbody = document.createElement('tbody');
app.appendChild(tbody);
app.update = function(data) {
tbody.innerHTML = '';
for (let row in data.tbody) {
let tr = document.createElement('tr');
for (let cell in row) {
let td = document.createElement('td');
td.textContent = cell;
tr.appendChild(td);
}
tbody.appendChild(tr);
}
}
document.body.appendChild(app);
app.update({
tbody: [
[1, 2, 3],
],
});
This is rather simpler, frankly. I prefer plain DOM for myself at this level.children() is supposed to take a function as argument and that function is supposed to return the list of children.
So basically:
children( function(el) {
return [
el.email = input(props({ type: 'email' })),
el.pass = input(props({ type: 'pass' })),
el.submit = button(text('Sign in'))
]
} )Also,
[
el.email = input(props({ type: 'email' }))
,
...
]is a neat idea. In one (expressive) line he is assigning the value to the array and also giving the child (in this case, email) a name and reference in its parent, so later he can just call el.email (in the onsubmit function).
children(function(el) {
var a, b, c;
a = input(props({ type: 'email' }));
el.email = a;
// etc
return [a, b, c];
})
Entirely guesswork but:
El is the form element you're defining children on.
You need to provide references to the elements you're creating so that you can use them later (el.email and friends) but you also need to return them to the children function in an ordered manner so that it can actually attach them into the DOM.I'm still not sure how I feel about that bit of code though but it sure is clever...
Point is to be able to design: 1) how you create component 2) how you update it Both 1) and 2) are just simple functions.