backbone.js - Underscore/Backbone template events -
in underscore.js templates, there way data of template click event? example:
geocoder.geocode({ 'address' : $(this.el).find("input[name=locationsearchtext]").val() }, function(results, status) { if (results && status && status == 'ok') { this.results = results; var list =_.template("<ul><% _.each(results, function(result){ %><li><%= result.formatted_address %></li><% })%></ul>"); $el.find("#search-results").html(list); }else{ alert("something went wrong!"); } });
and in backbone on view:
events: { 'click #search-results li': function(data){ 'the data of `result` passed template in each'} },
in past i've done stick data want in data-
attribute of element, this:
var list =_.template("<ul> <% _.each(results, function(result){ %> <li data-foo=\"<%= result.foo %>\"><%= result.formatted_address %></li> <% })%> </ul>");
and in callback retrieve this:
'click #search-results li': function(ev){ var foo = $(ev.currenttarget).data('foo'); }
but if need access whole result
object, rather storing in dom similar canjs's element callback does, store object jquery.data
on element:
this.results = results; var list =_.template("<ul><% _.each(results, function(result){ %><li><%= result.formatted_address %></li><% })%></ul>"); $el.find("#search-results").html(list).find('li').each(function(i, el) { $(el).data('result', results[i]); });
then retrieve in callback using $(ev.currenttarget).data('result')
.
Comments
Post a Comment