Skip to content

Commit

Permalink
change event to use target and relatedTarget (which more closely rese…
Browse files Browse the repository at this point in the history
…mbles actual event api)
  • Loading branch information
fat committed Sep 30, 2011
1 parent b827303 commit a0bf8b6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
7 changes: 4 additions & 3 deletions docs/javascript.html
Original file line number Diff line number Diff line change
Expand Up @@ -368,14 +368,15 @@ <h3>Events</h3>
<tbody>
<tr>
<td>change</td>
<td>This event fires on tab change. The event provides an additional parameter which holds the id of the previous tab and the id of the new current tab. This information is stored in an object with two properties called from and to, e.g. <code>{ to: '#home', from: '#profile' }</code>.</td>
<td>This event fires on tab change. Use <code>event.target</code> and <code>event.relatedTarget</code> to target the active tab and the previous active tab respectively.</td>
</tr>
</tbody>
</table>

<pre class="prettyprint linenums">
$('#.tabs').bind('changed', function (e, c) {
// do something with c.from and c.to ...
$('#.tabs').bind('change', function (e) {
e.target // activated tab
e.relatedTarget // previous tab
})</pre>
<h3>Demo</h3>
<ul class="tabs" data-tabs="tabs" >
Expand Down
12 changes: 8 additions & 4 deletions js/bootstrap-tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@

function tab( e ) {
var $this = $(this)
, href = $this.attr('href')
, $ul = $this.closest('ul')
, $controlled
, current = $ul.find('.active a').attr('href')
, href = $this.attr('href')
, previous

if (/^#\w+/.test(href)) {
e.preventDefault()
Expand All @@ -39,11 +38,16 @@
return
}

previous = $ul.find('.active a')[0]
$href = $(href)

activate($this.parent('li'), $ul)
activate($href, $href.parent())
$this.trigger("change", { from: current, to: href })

$this.trigger({
type: 'change'
, relatedTarget: previous
})
}
}

Expand Down
36 changes: 21 additions & 15 deletions js/tests/unit/bootstrap-tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,27 @@ $(function () {
+ '<li class="active"><a href="#home">Home</a></li>'
+ '<li><a href="#profile">Profile</a></li>'
+ '</ul>')
, changeCount = 0
, from
, to;

$tabsHTML.tabs().bind( "change", function (e, c){
from = c.from;
to = c.to;
changeCount++
})

$tabsHTML.tabs().find('a').last().click()

equals(from, "#home")
equals(to, "#profile")
equals(changeCount, 1)
, $target
, count = 0
, relatedTarget
, target

$tabsHTML
.tabs()
.bind( "change", function (e) {
target = e.target
relatedTarget = e.relatedTarget
count++
})

$target = $tabsHTML
.find('a')
.last()
.click()

equals(relatedTarget, $tabsHTML.find('a').first()[0])
equals(target, $target[0])
equals(count, 1)
})

})

0 comments on commit a0bf8b6

Please sign in to comment.