"select.country option@selected": function(a) {
if (a.context.country == country)
return 'selected';
else return '';
}
My list of options is static, part of the template. This directive
(not surprisingly) results in all options getting set to 'selected'.
This is a bit similar to the problem I had with id values in that they
are dependent on content inside the template, and removing it from
there means that I'll have template stuff in my code.
As I'm rendering this template each time the row is added and
appending it to the list of rows, I'm dropping the select from the
directives altogether and using jQuery to set the selection after the
render with:
$('#select.country:last').val(country);
I don't know if this can be improved, but I think I'll have to put it
down as a downside of using a DOM-based approach.
The directive is:
{
"option": {
"size <- sizes": {
".": "#{size.val} - #{size.name}",
"@value": "size.val",
"@selected": "size.sel"
}
}
}
Don't jump to quickly on harsh conclusions ;)
I use pure.js as the exclusive rendering engine for our web app. This
makes it ready for many situations.
But doesn't that mean that the list of options must be defined in the
data part? I want it to set the selection in a static list that's part
of the template. So if my template contains:
<select name="myselect">
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
<option value="d">D</option>
</select>
I want to be able to able to provide data like:
data = {"selectval": "c"};
and a simple directive to map that to the selected value (made-up
syntax!):
directive = {"select.myselect=": "selectval"};
and have it render to:
<select class="myselect" name="myselect">
<option value="a">A</option>
<option value="b">B</option>
<option value="c" selected="selected">C</option>
<option value="d">D</option>
</select>
As far as I can see this isn't possible without reading the values
from the option markup (because otherwise you can't match the option's
value to the data), which is the same thing I was on about in the [id]
conversation.
But I'm not sure it has anything better than a DOM manipulation like
you did.