From 474471b83140350de320f3ba940bd7dd8326b32e Mon Sep 17 00:00:00 2001 From: Carl Porth Date: Wed, 26 Sep 2012 23:11:03 -0700 Subject: [PATCH 1/2] bind data-api events to document instead of body to allow body replacement --- js/bootstrap-alert.js | 2 +- js/bootstrap-button.js | 2 +- js/bootstrap-carousel.js | 2 +- js/bootstrap-collapse.js | 2 +- js/bootstrap-dropdown.js | 2 +- js/bootstrap-modal.js | 2 +- js/bootstrap-tab.js | 2 +- js/bootstrap-typeahead.js | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/js/bootstrap-alert.js b/js/bootstrap-alert.js index 8500bd273c..ea3209ed19 100644 --- a/js/bootstrap-alert.js +++ b/js/bootstrap-alert.js @@ -84,7 +84,7 @@ * ============== */ $(function () { - $('body').on('click.alert.data-api', dismiss, Alert.prototype.close) + $(document).on('click.alert.data-api', dismiss, Alert.prototype.close) }) }(window.jQuery); \ No newline at end of file diff --git a/js/bootstrap-button.js b/js/bootstrap-button.js index cc2d0048fd..4c0b725c7a 100644 --- a/js/bootstrap-button.js +++ b/js/bootstrap-button.js @@ -86,7 +86,7 @@ * =============== */ $(function () { - $('body').on('click.button.data-api', '[data-toggle^=button]', function ( e ) { + $(document).on('click.button.data-api', '[data-toggle^=button]', function ( e ) { var $btn = $(e.target) if (!$btn.hasClass('btn')) $btn = $btn.closest('.btn') $btn.button('toggle') diff --git a/js/bootstrap-carousel.js b/js/bootstrap-carousel.js index 0d7167e1c2..8751b320cc 100644 --- a/js/bootstrap-carousel.js +++ b/js/bootstrap-carousel.js @@ -164,7 +164,7 @@ * ================= */ $(function () { - $('body').on('click.carousel.data-api', '[data-slide]', function ( e ) { + $(document).on('click.carousel.data-api', '[data-slide]', function ( e ) { var $this = $(this), href , $target = $($this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7 , options = !$target.data('modal') && $.extend({}, $target.data(), $this.data()) diff --git a/js/bootstrap-collapse.js b/js/bootstrap-collapse.js index 7345758000..781e274374 100644 --- a/js/bootstrap-collapse.js +++ b/js/bootstrap-collapse.js @@ -144,7 +144,7 @@ * ==================== */ $(function () { - $('body').on('click.collapse.data-api', '[data-toggle=collapse]', function (e) { + $(document).on('click.collapse.data-api', '[data-toggle=collapse]', function (e) { var $this = $(this), href , target = $this.attr('data-target') || e.preventDefault() diff --git a/js/bootstrap-dropdown.js b/js/bootstrap-dropdown.js index 0ef9b0f9d2..5a2766fdea 100644 --- a/js/bootstrap-dropdown.js +++ b/js/bootstrap-dropdown.js @@ -141,7 +141,7 @@ $(function () { $('html') .on('click.dropdown.data-api touchstart.dropdown.data-api', clearMenus) - $('body') + $(document) .on('click.dropdown touchstart.dropdown.data-api', '.dropdown form', function (e) { e.stopPropagation() }) .on('click.dropdown.data-api touchstart.dropdown.data-api' , toggle, Dropdown.prototype.toggle) .on('keydown.dropdown.data-api touchstart.dropdown.data-api', toggle + ', [role=menu]' , Dropdown.prototype.keydown) diff --git a/js/bootstrap-modal.js b/js/bootstrap-modal.js index d53f13a00c..8a40c2d4e6 100644 --- a/js/bootstrap-modal.js +++ b/js/bootstrap-modal.js @@ -216,7 +216,7 @@ * ============== */ $(function () { - $('body').on('click.modal.data-api', '[data-toggle="modal"]', function ( e ) { + $(document).on('click.modal.data-api', '[data-toggle="modal"]', function ( e ) { var $this = $(this) , href = $this.attr('href') , $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))) //strip for ie7 diff --git a/js/bootstrap-tab.js b/js/bootstrap-tab.js index 4fb3c38399..14387a1bf4 100644 --- a/js/bootstrap-tab.js +++ b/js/bootstrap-tab.js @@ -126,7 +126,7 @@ * ============ */ $(function () { - $('body').on('click.tab.data-api', '[data-toggle="tab"], [data-toggle="pill"]', function (e) { + $(document).on('click.tab.data-api', '[data-toggle="tab"], [data-toggle="pill"]', function (e) { e.preventDefault() $(this).tab('show') }) diff --git a/js/bootstrap-typeahead.js b/js/bootstrap-typeahead.js index 78cbe9faa5..ab1d66879d 100644 --- a/js/bootstrap-typeahead.js +++ b/js/bootstrap-typeahead.js @@ -298,7 +298,7 @@ * ================== */ $(function () { - $('body').on('focus.typeahead.data-api', '[data-provide="typeahead"]', function (e) { + $(document).on('focus.typeahead.data-api', '[data-provide="typeahead"]', function (e) { var $this = $(this) if ($this.data('typeahead')) return e.preventDefault() From e1f6458e3640a628cdb6b2e4c63950777d46e141 Mon Sep 17 00:00:00 2001 From: Carl Porth Date: Thu, 27 Sep 2012 15:00:02 -0700 Subject: [PATCH 2/2] don't wait for ready when binding events to document --- js/bootstrap-alert.js | 4 +--- js/bootstrap-button.js | 10 ++++------ js/bootstrap-carousel.js | 14 ++++++-------- js/bootstrap-collapse.js | 18 ++++++++---------- js/bootstrap-dropdown.js | 13 +++++-------- js/bootstrap-modal.js | 24 +++++++++++------------- js/bootstrap-tab.js | 8 +++----- js/bootstrap-typeahead.js | 12 +++++------- 8 files changed, 43 insertions(+), 60 deletions(-) diff --git a/js/bootstrap-alert.js b/js/bootstrap-alert.js index ea3209ed19..b0bdc4b7d9 100644 --- a/js/bootstrap-alert.js +++ b/js/bootstrap-alert.js @@ -83,8 +83,6 @@ /* ALERT DATA-API * ============== */ - $(function () { - $(document).on('click.alert.data-api', dismiss, Alert.prototype.close) - }) + $(document).on('click.alert.data-api', dismiss, Alert.prototype.close) }(window.jQuery); \ No newline at end of file diff --git a/js/bootstrap-button.js b/js/bootstrap-button.js index 4c0b725c7a..dbb2c08ed3 100644 --- a/js/bootstrap-button.js +++ b/js/bootstrap-button.js @@ -85,12 +85,10 @@ /* BUTTON DATA-API * =============== */ - $(function () { - $(document).on('click.button.data-api', '[data-toggle^=button]', function ( e ) { - var $btn = $(e.target) - if (!$btn.hasClass('btn')) $btn = $btn.closest('.btn') - $btn.button('toggle') - }) + $(document).on('click.button.data-api', '[data-toggle^=button]', function (e) { + var $btn = $(e.target) + if (!$btn.hasClass('btn')) $btn = $btn.closest('.btn') + $btn.button('toggle') }) }(window.jQuery); \ No newline at end of file diff --git a/js/bootstrap-carousel.js b/js/bootstrap-carousel.js index 8751b320cc..6b01f763b4 100644 --- a/js/bootstrap-carousel.js +++ b/js/bootstrap-carousel.js @@ -163,14 +163,12 @@ /* CAROUSEL DATA-API * ================= */ - $(function () { - $(document).on('click.carousel.data-api', '[data-slide]', function ( e ) { - var $this = $(this), href - , $target = $($this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7 - , options = !$target.data('modal') && $.extend({}, $target.data(), $this.data()) - $target.carousel(options) - e.preventDefault() - }) + $(document).on('click.carousel.data-api', '[data-slide]', function (e) { + var $this = $(this), href + , $target = $($this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7 + , options = !$target.data('modal') && $.extend({}, $target.data(), $this.data()) + $target.carousel(options) + e.preventDefault() }) }(window.jQuery); \ No newline at end of file diff --git a/js/bootstrap-collapse.js b/js/bootstrap-collapse.js index 781e274374..392e486a89 100644 --- a/js/bootstrap-collapse.js +++ b/js/bootstrap-collapse.js @@ -143,16 +143,14 @@ /* COLLAPSIBLE DATA-API * ==================== */ - $(function () { - $(document).on('click.collapse.data-api', '[data-toggle=collapse]', function (e) { - var $this = $(this), href - , target = $this.attr('data-target') - || e.preventDefault() - || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') //strip for ie7 - , option = $(target).data('collapse') ? 'toggle' : $this.data() - $this[$(target).hasClass('in') ? 'addClass' : 'removeClass']('collapsed') - $(target).collapse(option) - }) + $(document).on('click.collapse.data-api', '[data-toggle=collapse]', function (e) { + var $this = $(this), href + , target = $this.attr('data-target') + || e.preventDefault() + || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') //strip for ie7 + , option = $(target).data('collapse') ? 'toggle' : $this.data() + $this[$(target).hasClass('in') ? 'addClass' : 'removeClass']('collapsed') + $(target).collapse(option) }) }(window.jQuery); \ No newline at end of file diff --git a/js/bootstrap-dropdown.js b/js/bootstrap-dropdown.js index 5a2766fdea..f50aa64d62 100644 --- a/js/bootstrap-dropdown.js +++ b/js/bootstrap-dropdown.js @@ -138,13 +138,10 @@ /* APPLY TO STANDARD DROPDOWN ELEMENTS * =================================== */ - $(function () { - $('html') - .on('click.dropdown.data-api touchstart.dropdown.data-api', clearMenus) - $(document) - .on('click.dropdown touchstart.dropdown.data-api', '.dropdown form', function (e) { e.stopPropagation() }) - .on('click.dropdown.data-api touchstart.dropdown.data-api' , toggle, Dropdown.prototype.toggle) - .on('keydown.dropdown.data-api touchstart.dropdown.data-api', toggle + ', [role=menu]' , Dropdown.prototype.keydown) - }) + $(document) + .on('click.dropdown.data-api touchstart.dropdown.data-api', clearMenus) + .on('click.dropdown touchstart.dropdown.data-api', '.dropdown form', function (e) { e.stopPropagation() }) + .on('click.dropdown.data-api touchstart.dropdown.data-api' , toggle, Dropdown.prototype.toggle) + .on('keydown.dropdown.data-api touchstart.dropdown.data-api', toggle + ', [role=menu]' , Dropdown.prototype.keydown) }(window.jQuery); \ No newline at end of file diff --git a/js/bootstrap-modal.js b/js/bootstrap-modal.js index 8a40c2d4e6..0030ad7c50 100644 --- a/js/bootstrap-modal.js +++ b/js/bootstrap-modal.js @@ -215,21 +215,19 @@ /* MODAL DATA-API * ============== */ - $(function () { - $(document).on('click.modal.data-api', '[data-toggle="modal"]', function ( e ) { - var $this = $(this) - , href = $this.attr('href') - , $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))) //strip for ie7 - , option = $target.data('modal') ? 'toggle' : $.extend({ remote: !/#/.test(href) && href }, $target.data(), $this.data()) + $(document).on('click.modal.data-api', '[data-toggle="modal"]', function (e) { + var $this = $(this) + , href = $this.attr('href') + , $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))) //strip for ie7 + , option = $target.data('modal') ? 'toggle' : $.extend({ remote:!/#/.test(href) && href }, $target.data(), $this.data()) - e.preventDefault() + e.preventDefault() - $target - .modal(option) - .one('hide', function () { - $this.focus() - }) - }) + $target + .modal(option) + .one('hide', function () { + $this.focus() + }) }) }(window.jQuery); \ No newline at end of file diff --git a/js/bootstrap-tab.js b/js/bootstrap-tab.js index 14387a1bf4..8d73cd5515 100644 --- a/js/bootstrap-tab.js +++ b/js/bootstrap-tab.js @@ -125,11 +125,9 @@ /* TAB DATA-API * ============ */ - $(function () { - $(document).on('click.tab.data-api', '[data-toggle="tab"], [data-toggle="pill"]', function (e) { - e.preventDefault() - $(this).tab('show') - }) + $(document).on('click.tab.data-api', '[data-toggle="tab"], [data-toggle="pill"]', function (e) { + e.preventDefault() + $(this).tab('show') }) }(window.jQuery); \ No newline at end of file diff --git a/js/bootstrap-typeahead.js b/js/bootstrap-typeahead.js index ab1d66879d..dc0c7fa07a 100644 --- a/js/bootstrap-typeahead.js +++ b/js/bootstrap-typeahead.js @@ -297,13 +297,11 @@ /* TYPEAHEAD DATA-API * ================== */ - $(function () { - $(document).on('focus.typeahead.data-api', '[data-provide="typeahead"]', function (e) { - var $this = $(this) - if ($this.data('typeahead')) return - e.preventDefault() - $this.typeahead($this.data()) - }) + $(document).on('focus.typeahead.data-api', '[data-provide="typeahead"]', function (e) { + var $this = $(this) + if ($this.data('typeahead')) return + e.preventDefault() + $this.typeahead($this.data()) }) }(window.jQuery);