Created
December 7, 2012 09:52
-
-
Save mockee/4232210 to your computer and use it in GitHub Desktop.
Replace `click` with `tap`
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
/*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