Hi Mic,
I don't take any issue with the selector portion. I'm more concerned
about the readability of the append and loop syntax. I don't find them
intuitive.
For looping in particular, I would suggest using "animal in animals" or
"for(animal in animals)" instead of "animal<-animals" because from a
readability/learnability point of view, you would piggyback on the "for
... in" Javascript syntax. Yes, it is more verbose but I find the
readability gain is huge.
In general I would say that as soon as the left-hand-side changes from a
selector to some sort of action, the syntax becomes harder to digest.
In trying to create my own engine, I totally sympathize with the
difficulty of coming up with a good design. It's not easy...
Unfortunately, I don't have all the answers.
In my own experiments I found that, in some cases, coding directly to
JQuery (injecting JSON yourself) was slightly more verbose but a lot
more readable. For example, consider having to implement this in pure.js:
HTML:
<tbody>
<tr class="html template company">
<td class="name"></td>
<td>
<div class="ui checkbox">
<input type="checkbox"/><label
class="label"></label>
</div>
</td>
</tr>
</tbody>
JS:
var nextId = 0;
// Reset the template
var template = $("tbody > .html .template").detach();
var root = $("tbody");
root.empty().append(template);
companies.forEach(function(company, index)
{
var instance = template.clone();
++nextId;
var currentId = "checkbox-" + nextId;
var company = $(".company");
instance.find(".name").text(
company.name);
instance.find("input").attr("id", currentId);
instance.find("label").attr("for", currentId);
root.append(instance);
}
Notice that I am also taking care of resetting the template in case I
need to inject into the same HTML multiple times (if the user clicks the
refresh button).
Gili