Created
May 21, 2012 15:41
-
-
Save gryzzly/2762965 to your computer and use it in GitHub Desktop.
Backbone touch / click events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var down = "touchup" in window ? "touchup" : "click"; | |
var abstractView = Backbone.View.extend({ | |
// we don't want both click and touch handlers | |
// delegated on touch-enabled devices | |
events: function () { | |
"click .toggler" : "foo", | |
"touchup .toggler" : "bar" | |
}, | |
initialize: function () { | |
// we can do this instead | |
this.events = this.events || {}; | |
this.events[down + ' .toggler'] = "foo"; | |
// … list more events here | |
this.delegateEvents(); | |
} | |
}); | |
// This does solve the problem but this doesn't look good | |
// | |
// We could do something like jQuery special event, but Zepto, | |
// that is used on mobile devices doesn't support special events | |
// | |
// So seamless "down .toggler" : "foo", that would work by registering | |
// "special" jQuery event will not work with Zepto |
Another take on this same problem, a bit more elegant: https://gist.github.com/2763480
I created a plugin for this https://github.com/tuxracer/touchclick Then you can just do:
var FooView = Backbone.View.extend({
events: {
"touchclick .bar" : "bar",
"touchclick .baz" : "baz"
}
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is an ugly-ass piece of shit that I do