Anything special required to get jGestures working with Backbone.js?
-
24-06-2021 - |
質問
I am attempting to get jGestures working with a Backbone.js application without much luck.
I am including my scripts at the bottom of the page body.
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript" src="jgestures.min.js"></script>
<script type="text/javascript" src="underscore.js"></script>
<script type="text/javascript" src="backbone.js"></script>
<script type="text/javascript" src="backbone.localStorage.js"></script>
<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="src/app_root.js"></script>
<script type="text/javascript" src="src/app_beverage.js"></script>
<script type="text/javascript" src="src/app_order.js"></script>
<script type="text/javascript" src="src/app_people.js"></script>
<script type="text/javascript" src="src/app_router.js"></script>
And attaching the gesture handler like so.
events: {
"tapone .test": "something"
}
I know the event handler is attached correctly because I can trigger it manually via the console (on desktop, not a mobile device).
$(".test").trigger("tapone");
But running the code in iOS 5 it looks like the jGestures code is not generating the touch events. Is there someone else I need to do to trigger these touch events? I am using the Backbone router so maybe I need to init the library again after updating the DOM?
解決
Backbone.View.events
delegates directly over jQuery.bind
and jQuery.delegate
, look in the Backbone.View.delegateEvents' code.
So if your event binding definition can work like this:
$(<your View.el DOM element>).delegate(selector, eventName, method);
Then it should work with Backbone.
For example in your case you can try:
$(<your View.el DOM element>).delegate( ".test", "tapone", function(){ console.log( "hi! tapone" ); } );
Try to define the event binding directly in jQuery and see if doing it this way the event is triggered when expected, if it doesn't then the problem is out of Backbone, if it does then the problem should be in your implementation. I think in neither case the problem will be in Backbone itself.