Hi I was wondering how I handle the remove hover state using backbone and js
currently I have
events: {
"hover .info" : "hover"
},
hover:(e) =>
$(e.currentTarget).css("background-color", "#333")
I was wondering how I would handle the event where i move my mouse away from the element with class .info
If i do standard coffee script inside to do this inside the hover: event handler it requires 2 hovers for it to work.
I basically want to imitate
$(".info").hover(
function() {
$(this).css("background-color", "#333")
},
function() {
$(this).css("background-color", "#F3F")
},
});
Thanks
You are building applications that will follow an Event Driven Architecture. You have several disparate systems that all generate events and that need to respond to events. Event driven systems have come in a number of different types and styles.
Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.
BackboneJS allows developing of applications and the frontend in a much easier way by using JavaScript functions. BackboneJS provides various building blocks such as models, views, events, routers and collections for assembling the client side web applications.
From the jQuery docs:
Calling
$(selector).hover(handlerIn, handlerOut)
is shorthand for:
$(selector).mouseenter(handlerIn).mouseleave(handlerOut);
So I think the best way to handle this with Backbone is just to set two events (sorry, I don't believe in CoffeeScript):
events: {
"mouseenter .info" : "hoverOn",
"mouseleave .info" : "hoverOff"
},
hoverOn: function(e) { ... },
hoverOff: function(e) { ... }
Alternatively, you could use the single argument syntax, which takes one function and calls it on both in and out - this works well with .toggle()
and toggleClass()
.
There is a version of hover()
that takes one callback function:
Description: Bind a single handler to the matched elements, to be executed when the mouse pointer enters or leaves the elements.
This is the version of hover
that will get used by Backbone. So you could handle this with toggleClass
and a couple CSS classes instead of directly messing around with the css
:
hover:(e) =>
$(e.currentTarget).toggleClass('dark-gray')
The default #F3F
color would be set on the element by default and you'd have:
.dark-gray {
background-color: #333
}
in your stylesheets. If you cannot for some reason use toggleClass
, you'd have to bind to mouseenter
and mouseleave
individually.
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