I am newin Backbone, and try to write some simple applications to learn backbone.
The application contains a list of items, and user can add/delete/ items, when user hover one of the item view, the item should changed its color to red,othewise, its color should be black.
var ItemView = Backbone.View.extend({
tagName: 'li',
events: {
'click a.delete': 'onDelete',
'mouseover': 'onHoverIn',
'mouseout': 'onHoverOut'
},
template: _.template($('#tpl-item').html()),
initialize: function () {
this.model.on("change", this.render, this);
},
render: function () {
var item = this.model;
this.$el.css("color", item.get("hover") ? "red" : "black").html(this.template(item.toJSON()));
return this;
},
onDelete: function () {
this.model.destroy();
},
onHoverIn: function () {
this.model.set("hover", true);
},
onHoverOut: function () {
this.model.set("hover", false);
}
});
This is the full codes of the app: jfiddle live example
However, it works while I move the mouse over the items one by one slowly, once I move quickly, I found that there may be more than one items are colored with red, which is not expected.
What's going on?