Unobtrusive Knockout Template Question
I've been experimenting with an unobtrusive Knockout data-binding jQuery plugin. Follow the link here.
I cannot seem to figure out how to keep the "data-bind" attribute out of a template, though. I can't decide whether or not it should even be done, either. I just have a hunch.
Example template:
<script id="storeTemplate" type="text/x-jquery-tmpl">
<li>
<div class="storeTitle" data-bind="click: select">${storeTitle}</div>
</li>
</script>
I'm thinking that it might be a good idea to pull data-bind="click: select"
out of there. Does anyone have an idea a开发者_StackOverflows to how to do that? I've tried $(".storeTitle").dataBind( { click: "select" } );
A jQuery selector only selects objects that have already been created in the DOM, yet the elements we want to edit are not part of the DOM yet. Also, I would like to avoid applying bindings more than once.
It seems I just sort of answered my own question. Considering that the elements in the template are actually sitting in the DOM (just in string form) I could just modify the string and add the data-bind="click: select". Rather than doing string manipulation, a coworker suggested I just temporarily insert the template text as innerHTML to add it to the DOM, modify it using the plugin, insert the modified version back into the template as text and apply bindings.
精彩评论