Closed jorrit closed 13 years ago
Templates are about string concatenation. You can do data-item="${foo}" where foo is any expression that returns a string, and it will work fine. If it returns an object, then it is converted by toString(). data attributes are supported but treated in the same way as other attributes...
I did not intend this issue as a bug, but as a feature request, I should have been more clear about that. Would it be a lot of effort to check if an assignment is made to an attribute that starts with data-?
Can you describe the feature you want? Are you suggesting that if it is a data attribute, it adds it to jQuery,data? And are you suggesting that it adds objects as objects, without doing a string conversion...?
The first option sounds like what I want. The end result would be that calling .data('item') on the divs with class item from my example would return the item object that was used in that loop.
OK. For the specific scenario, you have $.tmplItem, and .tmplItem already which allow you get the item from any element rendered by that template. So you don't need to go through a special data-item feature.
More generally, I'm not sure the special semantics are a good idea. A template is to do with string concatenation, and should be able to render as an HTML string, so with the following template
<div class="item" data-bar="${bar}">
you would get the rendered HTML
<div class="item" data-bar="valueOfBar">
Inserting that HTML as innerHTML will make the string value of Bar available through .data (that is already the case because .data detects the data attributes) - but will not allow you to get back to the object 'prior to string conversion'.
The new implementation of jQuery Templates is JsRender and JsViews, which makes the string concatenation role of templates more explicit.
In this case, that doesn't work because I need the object in the loop and tplItem gives the object that was passed to the entire template. But now I think of it, I can probably use a subtemplate in the loop and call tplItem on the subtemplate. In any case, I'll close this issue. Thanks for your time.
Yes, use {{tmpl}} rather than {{each}}. Even with your feature, you would not have an item for each iteration of the {{each}}, so would need to use {{tmpl}}
It would be nice to be able to write the following:
Currently, date-item would just contain the string [object Object] or something like that.