Optional component parameters

28 views
Skip to first unread message

Alx Dark

unread,
Aug 26, 2015, 3:14:26 PM8/26/15
to KnockoutJS
I've created a custom element that wraps an input element. I'd like to have the optional ability to pass a placeholder parameter, and if it's present, to set the placeholder attribute. But for some uses I don't want to include a placeholder.

e.g. referencing

<ui-input params="valueObs: someObs, placeholder: 'Label'"></ui-input>

then in the component

<input type="text" data-binding="attr:{placeholder: placeholder}, textInput: valueObs"/>

This throws an error if the placeholder parameter isn't provided. Trying to default in the data-binding doesn't seem to work as the reference to placeholder is itself not there. Is it possible to do something like this, or do I have to add placeholder to all existing references to the ui-input element?

I have may elements that could well use some strategy for having optional parameters like this.

Thanks in advance,

Alx

Gunnar Liljas

unread,
Aug 26, 2015, 4:21:32 PM8/26/15
to knock...@googlegroups.com

Alx Dark

unread,
Aug 26, 2015, 4:24:25 PM8/26/15
to KnockoutJS
Okay thanks. I hadn't created view models for these but it's well worth it to get this functionality.

Alx

On Wednesday, August 26, 2015 at 1:21:32 PM UTC-7, Gunnar Liljas wrote:

Michael Best

unread,
Aug 26, 2015, 8:04:11 PM8/26/15
to KnockoutJS
You can bind to $data.placeholder to make it work with undefined properties. http://jsfiddle.net/fn20jrwq/2/

-- Michael

Alx Dark

unread,
Aug 26, 2015, 8:48:03 PM8/26/15
to KnockoutJS
Ah, this is exactly what I was hoping for, this is so useful. Thanks!

Alx
Reply all
Reply to author
Forward
0 new messages