Skip to content

Instantly share code, notes, and snippets.

@mockee
Created December 7, 2012 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mockee/4232210 to your computer and use it in GitHub Desktop.
Save mockee/4232210 to your computer and use it in GitHub Desktop.
Replace `click` with `tap`
/*global _:true, Backbone:true*/
define('backbone', ['backbone-src'], function() {
if (!('ontouchstart' in window)) { return Backbone }
require('mod/touch', function(){})
var delegateEventSplitter = /^(\S+)\s*(.*)$/
function getValue(object, prop) {
if (!(object && object[prop])) { return null }
return _.isFunction(object[prop]) ? object[prop]() : object[prop]
}
_.extend(Backbone.View.prototype, {
delegateEvents: function(events) {
if (!(events || (events = getValue(this, 'events')))) { return }
this.undelegateEvents()
var suffix = '.delegateEvents' + this.cid
_(events).each(function(method, key) {
if (!_.isFunction(method)) {
method = this[events[key]]
}
if (!method) {
throw new Error('Method "' + events[key] + '" does not exist')
}
var match = key.match(delegateEventSplitter)
, eventName = match[1]
, selector = match[2]
method = _.bind(method, this)
if (eventName === 'click' && selector !== '') {
this.$el.on('tap', selector, method)
} else {
eventName += suffix
if (selector === '') {
this.$el.bind(eventName, method)
} else {
this.$el.on(eventName, selector, method)
}
}
}, this)
}
, undelegateEvents: function() {
this.$el.unbind('.delegateEvents' + this.cid)
}
})
return Backbone
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment