]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
Added changed event to bootstrap-tabs.js 237/head
authorKasper Bøgebjerg Pedersen <kasperbp@gmail.com>
Sun, 18 Sep 2011 08:15:24 +0000 (10:15 +0200)
committerKasper Bøgebjerg Pedersen <kasperbp@gmail.com>
Sun, 18 Sep 2011 08:15:24 +0000 (10:15 +0200)
docs/javascript.html
js/bootstrap-tabs.js
js/tests/unit/bootstrap-tabs.js

index f8ef930c6ee483280439ab5385b30a7e3b604acb..2495e491dba1f15554b5d4332f64e631e4f67cd0 100644 (file)
@@ -357,6 +357,26 @@ $('#my-modal').bind('hidden', function () {
   })
 &lt;/script&gt;</pre>
           </p>
+                                       <h3>Events</h3>
+                                       <table class="zebra-striped">
+                                         <thead>
+                                          <tr>
+                                            <th style="width: 150px;">Event</th>
+                                            <th>Description</th>
+                                          </tr>
+                                         </thead>
+                                         <tbody>
+                                          <tr>
+                                            <td>changed</td>
+                                            <td>This event fires when the tabs are changed. The event provides an additional parameter which holds 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>. This event allows you load and change content of the tabs on request.</td>
+                                          </tr>
+                                         </tbody>
+                                       </table>
+
+                                       <pre class="prettyprint linenums">
+$('#.tabs').bind('changed', function (e, c) {
+  // do something with c.from and c.to ...
+})</pre>
           <h3>Demo</h3>
           <ul class="tabs" data-tabs="tabs" >
             <li class="active"><a href="#home">Home</a></li>
index 807b366a354de2f9ab5180b3619ee5d7095a715a..380ded17c8494b9832f86dedb93e5ba0d7b92ed4 100644 (file)
       , href = $this.attr('href')
       , $ul = $(e.liveFired)
       , $controlled
+      , current = $ul.find('.active a').attr('href')
 
     if (/^#\w+/.test(href)) {
       e.preventDefault()
 
-      if ($this.hasClass('active')) {
+      if ($this.parent('li').hasClass('active')) {
         return
       }
 
@@ -42,6 +43,7 @@
 
       activate($this.parent('li'), $ul)
       activate($href, $href.parent())
+      $this.trigger("changed", {from:current, to:href})
     }
   }
 
index 2ee6761edae06d004b754caca9fa5ea4deb1a5e0..0b92b94db07b94e7e5636dcd28867d7a8d0ccbbb 100644 (file)
@@ -11,39 +11,61 @@ $(function () {
       })
 
       test("should activate element by tab id", function () {
-        var tabsHTML = '<ul class="tabs">'
+        var $tabsHTML = $('<ul class="tabs">'
           + '<li class="active"><a href="#home">Home</a></li>'
           + '<li><a href="#profile">Profile</a></li>'
-          + '</ul>'
+          + '</ul>')
 
 
         $('<ul><li id="home"></li><li id="profile"></li></ul>').appendTo("#qunit-runoff")
 
-        $(tabsHTML).tabs().find('a').last().click()
+        $tabsHTML.tabs().find('a').last().click()
         equals($("#qunit-runoff").find('.active').attr('id'), "profile")
 
-        $(tabsHTML).tabs().find('a').first().click()
+        $tabsHTML.tabs().find('a').first().click()
         equals($("#qunit-runoff").find('.active').attr('id'), "home")
 
         $("#qunit-runoff").empty()
       })
 
       test("should activate element by pill id", function () {
-        var pillsHTML = '<ul class="pills">'
+        var $pillsHTML = $('<ul class="pills">'
           + '<li class="active"><a href="#home">Home</a></li>'
           + '<li><a href="#profile">Profile</a></li>'
-          + '</ul>'
+          + '</ul>')
 
 
         $('<ul><li id="home"></li><li id="profile"></li></ul>').appendTo("#qunit-runoff")
 
-        $(pillsHTML).pills().find('a').last().click()
+        $pillsHTML.pills().find('a').last().click()
         equals($("#qunit-runoff").find('.active').attr('id'), "profile")
 
-        $(pillsHTML).pills().find('a').first().click()
+        $pillsHTML.pills().find('a').first().click()
         equals($("#qunit-runoff").find('.active').attr('id'), "home")
 
         $("#qunit-runoff").empty()
       })
+      
+      test( "should trigger changed event on activate", function () {
+        var $tabsHTML = $('<ul class="tabs">'
+          + '<li class="active"><a href="#home">Home</a></li>'
+          + '<li><a href="#profile">Profile</a></li>'
+          + '</ul>')
+          , changeCount = 0
+          , from
+          , to;
+          
+        $tabsHTML.tabs().bind( "changed", 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)
+      })
 
 })
\ No newline at end of file