-
Notifications
You must be signed in to change notification settings - Fork 643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[v4] Allow setState to accept an Event object? #528
Comments
Just throwing this out there is a possible alternative: <script>
module.exports = {
handleNameChanged(value) {
this.state.name = value.toUpperCase();
if (this.state.name.length > 10) {
this.state.invalidName = true;
}
}
}
</script>
<input value:bind('name')/>
<input value:bind('name', 'handleNameChanged')>
<!-- OR: -->
<input bind('name')/>
<input bind('name', 'handleNameChanged')> In theory, this could be implemented in user land (via a combination of a custom compile-time transform and a small runtime) and if it proves to be valuable then we could pull it into the core marko. Thoughts? |
For reference: |
Coming from Mithril.js, I always liked teaching two-way data binding in this manner: var myData = 'initial'
var myComponent = {
view: function () {
return m('input[type=text]', {
value: myData
oninput: (e) => { myData = e.currentTarget.value }
})
}
} The value in this is it uses plain DOM concepts ( In other words, there's very little magic to it. So far, Marko has also avoided data-flow magic for the most part, and is thus very close to plain HTML and JS; it's precisely why I was able to pick it up so easily. HTML-based frameworks (including angular & vue) do not have the benefit of being plain JS, which is why those other frameworks add easy, "proprietary" ways to bind data – they want to take the chore out of it. However, I don't think it's the best approach to take for these reasons:
I think changing |
Here are some notes from various discussions: <script>
module.exports = {
onInput() {
this.state.orders = [
{ id: '0', title: 'Foo', shipped: true },
{ id: '1', title: 'Bar', shipped: false },
]
},
handleShippedChanged() {
this.state['orders.${i}.shipped'] = foo;
}
}
</script>
<ul>
<li for(i, order in orders)>
<input type='checkbox' checked:bind('orders.${i}.shipped', 'handleShippedChanged')> Shipped?
</li>
</ul> <script>
module.exports = {
onInput() {
this.state.orders = [
{ id: '0', title: 'Foo', shipped: true },
{ id: '1', title: 'Bar', shipped: false },
]
}
toggleShipped(i, event, input) {
// Add support for a settting a property chain while maintaining
// immutability
this.setState('orders', i, 'shipped', input.checked);
// Or, the long way
var orders = this.state.orders = [].concat(this.state.orders);
var order = orders[i] = Object.assign({}, orders[i]); // Create a clone of the order
order.shipped = input.checked;
}
}
</script>
<ul>
<li for(i, order in orders)>
<input type='checkbox' checked=order.shipped on-change('setState', i)> Shipped?
</li>
</ul> |
I'm starting to really like the enhanced Another reason I think it's important to keep the data bind directions separate is to allow for different kinds of data binding without creating more esoteric language constructs. For example, if you have an |
@mindeavor Our thinking was The issue with We could add a new method: |
@mlrawlings did you mean |
Ideally The [non-mutually exclusive] options I see are:
|
Checking in again – that first bullet point would be very handy in small components and demos :) |
Currently, our goal is to simplify the runtime and remove these kinds of special cases. I don't believe this issue will be addressed, unless maybe by two-way binding support. |
New Feature
Perhaps related to #125
Description
Allow the signature
setState(key, value)
to accept an Event object as its value (e
), causingsetState
to extracte.target.value
.Context
Forcing two-way data binding to be explicit is good in general, but for very basic cases it's a chore. For example, take the following component:
For this very basic task, I had to write a
setPassword
handler to capture user input into state. If I had more than input, I would have to either write more handlers, or write a generic handler that differentiates by some kind of key. In any case, it's something I have to write and think about every time I need to handle user input.However, if
setState
were to accept an event as its value, it would allow me to write the following instead:This avoids the need to write any handler at all, and remains very readable for what it does. For anything that needs something more complicated – such as scrubbing out invalid characters for a phone number input – it's not hard to fall back to the "write your own handler" way of doing it.
Open Questions
Is this something you're interested in working on?
Yes
The text was updated successfully, but these errors were encountered: