I'm trying to get the click
event to fire, but it's not working. Maybe someone can see something I can't.
ConnectionView = GlobalView.extend({
tagName: 'div',
events: {
"click .social-links": "check"
},
initialize: function() {
this.render();
this.model.bind("change", this.render);
},
render: function() {
// Compile the template using underscore
var template = _.template($("#connection-template").html(), this.model.toJSON());
// Load the compiled HTML into the Backbone "el"
$(this.el).html(template);
},
check: function(e) {
e.preventDefault();
alert("woot");
}
});
Here is the template that it's pulling:
<script id="connection-template" type="text/template">
<a id="link-<%= alt %>" class="social-links" href="<%= url.replace('||state||', state).replace('||timestamp||', time) %>">add <%= name %></a>
</script>
Here is the view that puts the ConnectionView into the DOM:
ConnectionsView = GlobalView.extend({
tagName: 'div',
initialize: function(){
this.collection.bind("reset", this.render, this);
},
render: function(){
var $connections = $("<div class='external-accounts'>");
this.collection.each(function (model) {
var conn = new ConnectionView({model: model});
$connections.append(conn.el);
});
// Compile the template using underscore
var template = _.template($("#connections-template").html());
// Load the compiled HTML into the Backbone "el"
$(this.el).html(template({
connectionsList: $connections.outer()
}));
},
destroy: function() {
this.constructor.__super__.destroy.apply(this);
}
});
Your problem is how you're filling in your
ConnectionsView
'sel
. You're doing this:I have no idea what
.outer()
is all about but it doesn't matter that much. The important thing is that everything is going through the compiled Underscoretemplate()
function and that means that everything will end up getting converted to strings along the way into the page. Once your DOM elements in$connections
are in a string, the event bindings are lost and nothing works anymore.Consider this example:
In there we do this:
to add your
ConnectionView
to the page. The events don't work in there becausetemplate()
returns a string and that string gets parsed into DOM elements which end up in the page. But, if we add the$connections
straight to the page:using something like this:
the events work as expected.
So now we know what went wrong. But how do we fix it? All we need to do is adjust your
ConnectionsView
to add$connections
directly to the DOM with something like this:Push the
<div class="external-accounts">
into theConnectionsView
template and insert theConnectionView
s directly into it. This way you're always working with DOM elements, strings don't know about events but DOM elements do.