using "data" with jquery tempates and "each"
I'm building web content using jquery templates and json and I'm looking for a clean(er) way to get the json data associated with a click event on content that was built using those templates. The tmplItem().data that is stored is the array of objects for that template. When an item in that list is clicked, I want to pass just the json data for that item to a click event handler.
Currently I'm doing this:
<script id="ribbonTemplate" type="x-jquery-tmpl">
{{开发者_开发百科each(i, item) ribbon_data}}
<img id='${i}' src="${thumbnail_url}" alt="${content_url}"/>
{{/each}}
</script>
and
$(ribbonDiv).live('click', function(e){
var clickItem = $(e.target);
var tmplItem = clickItem.tmplItem();
var imgId = clickItem.attr('id');
var jsonBackingData = tmplItem.data['ribbon_data'][imgId];
clickEvent(jsonBackingData);
});
This works but it seems a bit clunky. Is there a way that doesn't depend on using the id selector?
精彩评论