Asynchronous jQuery templates
I would like to be able to create jQuery templates that gets filled in by data fetched by some asynch queries, for this I need to refer back to elements I render within a template. I have come up with a solution using a generated class-name, which I use after rendering the template to insert the data, but this is a kludge. Is there a way to get a reference to the rendered element from within the template code? Here is the code I would like to get working:
<script>
function renderData(prduct_id, element){
getProductData(product_id, function(data){
$(e).empty();
$("#product-info-tmpl").tmpl({"data":data}).appendTo($(element));
});
}
</script>
<script id="product-info-loading-tmpl" type="text/x-jquery-tmpl">
loading
开发者_如何学Go//Here is where I'd need a reference to the rendered element
${renderData(product_id, this_element_when_rendered)}
</script>
Does this help, so in your case something with $item, maybe $item.parent or something...
Copied somewhere from http://api.jquery.com/template-tag-equal/
The $item and $data Template Variables The following variables are exposed to expression evaluation within templates:
$: The jQuery object. $item: The current template item - which allows access to $item.data, $item.parent, etc. as well as any user-defined values or methods passed in with the options map. $data: The current data item (equivalent to $item.data). Note: A template tag with content such as {{each}}...{{/each}} may expose additional variables to template evaluation within the content. In the case of {{each}}, for example, the additional template variables $value and $index are provided within the content of the {{each}} tag.
精彩评论