-
-
Save privman/2763480 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
// WARNING: untested | |
// Override the default View.extend to automatically parse the events object upon class definition. | |
Backbone.View.originalExtend = Backbone.View.extend; | |
Backbone.View.extend = function (protoProps, classProps) { | |
var downEvent = "touchup" in window ? "touchup" : "click"; | |
if (typeof(protoProps.events) === "object") { | |
// Replace the word "down" in the events hash with platform-specific event type | |
_.each(protoProps.events, function replaceDown(triggerString, functionName, events) { | |
var newTriggerStr = triggerStr.replace(/^down /, downEvent + ' '); | |
if (newTriggerStr !== triggerStr) { | |
events[newTriggerStr] = functionName; | |
delete events[triggerStr]; | |
} | |
}); | |
} | |
return Backbone.View.originalExtend(protoProps, classProps); | |
}; | |
var abstractView = Backbone.View.extend({ | |
events: { | |
"down .toggler" : "foo", | |
"down .toggler" : "bar" | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment