Skip to content
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

CheckedObserver truthy values and matcher coercion #92

Closed
fkleuver opened this issue Aug 21, 2018 · 10 comments
Closed

CheckedObserver truthy values and matcher coercion #92

fkleuver opened this issue Aug 21, 2018 · 10 comments
Assignees
Labels
API Docs Documentation needs to be added/improved/corrected In discussion Needs more feedback RFC Request For Comment - Potential enhancement that needs discussion and/or requires experimentation Topic: Binding Issues related to binding/observation
Milestone

Comments

@fkleuver
Copy link
Member

In fixing the CheckedObserver tests / logic for the SubscriberCollection PR I noticed a few things that might be up for improvement, but in order to keep that PR clean I'm adding some notes here instead.

If you set the value property of a checkbox to a non-string primitive and have the checked property bound to an array with that same non-string primitive, the corresponding input element would not be checked because the value property of a checkbox is always coerced to a string. Users might consider this unintuitive.

In the default matcher function:

(a, b) => a === b

We could coerce the value (a, which comes from the VM) to strings:

(a, b) => (a+'') === b

This would make the behavior consistent with how HTML behaves.
The performance impact here would be negligible since adding an empty string to a string is usually optimized by browser jit.

Furthermore, for similar reasons we could consider being more loose with the checked property comparison. Booleans have a tendency to get coerced to strings somewhere along the flow of binding.
So we might want to change this:

...
if (value === true) {
      element.checked = true;
...

To this:

...
if (value === true || value === 'true') {
      element.checked = true;
...

@EisenbergEffect @bigopon

@fkleuver fkleuver added enhancement RFC Request For Comment - Potential enhancement that needs discussion and/or requires experimentation and removed enhancement labels Aug 21, 2018
@EisenbergEffect
Copy link
Contributor

The looser comparison seems like it might be the way to go, rather than coercing values.

@fkleuver
Copy link
Member Author

fkleuver commented Aug 22, 2018

Can you elaborate?

You mean a == b instead of (a+'') === b or?

@EisenbergEffect
Copy link
Contributor

I meant this value === true || value === 'true'. Or is this two separate issues?

@fkleuver
Copy link
Member Author

fkleuver commented Aug 22, 2018

Yeah it's two separate ones. (a, b) => a === b is the default matcher function for radio input collections and checked arrays. value === true is for single primitive values

The primitive only responds to boolean at the moment. But since checked only responds to boolean, coercion numbers wouldn't make sense anyway, hence the || value === 'true'

The array is a bit broader because it simply looks for equality with the .value attribute, hence the a+''

@EisenbergEffect
Copy link
Contributor

We should get @jdanyow to provide feedback on this. I thought the model properties we set up handled keeping the original types. It was a long time ago that we addressed this issue though.

@fkleuver
Copy link
Member Author

Yep just ran into this with the unit tests. The model property doesn't have this problem so I would only apply the coercion logic for the value attribute. I've always seen the model as a way to support objects, not necessarily for the coercion.
It's possible to solve the coercion with model, but letting that be the only way seems counter-intuitive.

For example:

input.value = 42;
input.value === 42; // false - would become true

input.value = '42';
input.value === 42; // false - would become true

input.value = 42;
input.value === '42'; // true - stays true

input.value = 'true';
input.value === true; // false - would become true

etc..

@fkleuver fkleuver self-assigned this Sep 8, 2018
@bigopon
Copy link
Member

bigopon commented Oct 5, 2018

Just walked through the comments, I'm not sure how to look at this. Though altering behavior of input.value could be nice, I don't think it's a good idea as it's no longer predictable. That's for our core implementation. In the future, we can allow observer locator to be override-able / extendable and can experiment with all these maybe intuitive behaviors.

@fkleuver
Copy link
Member Author

fkleuver commented Oct 5, 2018

It's the other way around. Currently it's not predictable because the DOM will coerce values but the observer won't. I'm simply suggesting we should make the observer do the same coercions that the DOM does, so that it becomes predictable :)

@EisenbergEffect EisenbergEffect added this to the 0.5.0 milestone Oct 12, 2018
@davismj davismj added this to Discussion in Proposals Jan 23, 2019
@davismj
Copy link
Member

davismj commented Feb 18, 2019

Is there any reason we have to use the input.value property for these comparisons? Couldn't we handle all cases the same way we handle model.bind?

@fkleuver fkleuver added this to Triage in Work queue Oct 7, 2019
@fkleuver fkleuver moved this from Triage to Backlog in Work queue Oct 7, 2019
@fkleuver fkleuver added API Docs Documentation needs to be added/improved/corrected In discussion Needs more feedback Topic: Binding Issues related to binding/observation and removed packages/runtime labels Oct 7, 2019
@fkleuver fkleuver modified the milestones: v2.0-alpha, Backlog Jul 17, 2020
@bigopon
Copy link
Member

bigopon commented Mar 21, 2023

Implicitly converting the value types could be double edged, we better wait until the first request comes to have some concrete case based ideas.

@bigopon bigopon closed this as completed Mar 21, 2023
Work queue automation moved this from Backlog to Done Mar 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Docs Documentation needs to be added/improved/corrected In discussion Needs more feedback RFC Request For Comment - Potential enhancement that needs discussion and/or requires experimentation Topic: Binding Issues related to binding/observation
Projects
Proposals
  
Discussion
Work queue
  
Done
Development

No branches or pull requests

4 participants