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

Binding without values are change to string undefined #249

Closed
jcubic opened this issue Jan 13, 2017 · 9 comments
Closed

Binding without values are change to string undefined #249

jcubic opened this issue Jan 13, 2017 · 9 comments

Comments

@jcubic
Copy link

jcubic commented Jan 13, 2017

In current repl if you don't provide value for binding the value (in input tag) is undefiend and coerced to string so its value is "undefined" demo

if the value is undefiend it should be empty string in input, like in this demo

@jcubic jcubic changed the title Binidng without values are change to string undefined Binding without values are change to string undefined Jan 13, 2017
@jcubic
Copy link
Author

jcubic commented Jan 13, 2017

I think the problem is this code in output:

	addEventListener( input, 'input', inputChangeHandler );
	input.value = root.name;

I think it should be:

	addEventListener( input, 'input', inputChangeHandler );
	input.value = root.name || "";

@jcubic
Copy link
Author

jcubic commented Jan 13, 2017

and also this line, when updating input value:

if ( !input_updating ) input.value = root.name || "";

@jcubic
Copy link
Author

jcubic commented Jan 13, 2017

or better

input.value = typeof root.name == 'undefiend' ? "" : root.name;

so it will work when value is number 0 or boolean false. it will be converted to string "0" and "false" which is fine I think.

@proyb6
Copy link

proyb6 commented Jan 14, 2017

You like to pull a request?

@jcubic
Copy link
Author

jcubic commented Jan 14, 2017

I can take a look at the code and create PR if I'll found the solution to the problem in the code.

@jcubic
Copy link
Author

jcubic commented Jan 14, 2017

Created PR.

@Rich-Harris
Copy link
Member

Thanks for the PR @jcubic. I'm a little hesitant to go down this road — while it seems more convenient in this case, I worry that it's not a good general solution, since it doesn't necessarily translate well to other forms of two-way binding (#10), and can easily mask errors (e.g. if name was supposed to be username or something).

I think the best way to handle this case is to have a default value for name in the component, possibly in combination with development warnings (#13).

@arxpoetica
Copy link
Member

I can get behind that. It's certainly different than Ractive, but it forces a sort of declarative/specific measure on variable values—i.e., have variables respond according to actual definition. Please do add a console warning or something to that effect for ease of development, however.

@Rich-Harris
Copy link
Member

Development warnings are now implemented — if a property isn't declared for a binding, and the compiler is running with dev: true, an error will be thrown at runtime

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants