M Trying to give mouseover
event in my view of Backbone, here is my view :
Backbone.View.extend({
template :_.template( '<li class="<% if (refertype=="U"){%>info <% }else{%> access<%}%> main"><%=refername%>'+
'</li>'),
initialize: function() {
_.bindAll(this, 'render', 'close');
this.model.bind('change', this.render);
this.model.view = this;
},
events: {
"mouseover .main": "mouseovercard"
},
// Re-render the contents of the Card item.
render: function() {
this.el=this.template(this.model.toJSON());
$(".cards-list").append(this.el);
},
mouseovercard: function() {
console.log("hello world");
}
});
But when I am doing mouseover the main
class it is not showing hello world
, Please suggest what to do?
Tried Heikki Answer but mouseover not working ?
App.Backbone.CardView = Backbone.View.extend({
tagName: 'li',
className: 'main',
initialize: function() {
_.bindAll(this, 'render');
this.model.bind('change', this.render);
this.model.view = this;
},
events:{
"mouseover .main": "mouseovercard"
},
// Re-render the contents of the Card item.
render: function() {
$(this.el)
.removeClass('info access')
.addClass(this.model.get('refertype') == 'U' ? 'info' : 'access')
.text(this.model.get('refername'));
$(".cards-list").append(this.el);
},
mouseovercard: function() {
console.log("hello world");
}
});
You are replacing the view's root element where the events are bound to.
Try this instead:
Backbone.View.extend({
tagName: 'li',
className: 'main',
events: {
'mouseover': 'mouseovercard'
},
initialize: function() {
_.bindAll(this, 'render');
this.model.bind('change', this.render);
},
render: function() {
$(this.el)
.removeClass('info access')
.addClass(this.model.get('refertype') == 'U' ? 'info' : 'access')
.text(this.model.get('refername'));
return this;
},
mouseovercard: function() {
console.log('hello world');
}
});
http://documentcloud.github.com/backbone/#View-extend
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With