Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7869081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:12:11+00:00 2026-06-03T01:12:11+00:00

I am trying to build a very specific module that integrates Twitter bootstrap (for

  • 0

I am trying to build a very specific module that integrates Twitter bootstrap (for the tabs), jQuery (for ease of development) and knockout.js for client-side view models. The idea is that I have one HTML page, which has 3 div’s defined in it. Each div is a tab, with only one div visible at a particular time. When a div is displayed it should call a load method on a knockout.js viewmodel that is scoped to the tab and reload the data from the server.

I have written the following code (which works), however it is very specific to a particular tab in my application.

// only configure this once the DOM is ready for processing.
// this code snippet is very long winded and quite a hack, however it allows the content of a bootstrap.js
// tab to be reloaded when the tab is made visible. It does this by calling the LoadCategory() method on the
// knockout.js view model.
//
// it is also worth noting that the view model is bound using knockout to only descendents of the div that contains
// the tab contents. This is to ensure that we can have several knockout view models in one page without needing to
// worry about them interfering with each other.
$(document).ready(function () {
    // initialise the model...
    var todaysQuestionsModel = new ViewModel(categoryId);

    // if this tab is visible to begin with, load the view model.
    if ($('#todays-questions').hasClass('active')) {
        todaysQuestionsModel.LoadCategory();
    }

    // only apply these bindings to the elements that descend from the div that contains this tab.
    ko.applyBindings(todaysQuestionsModel, document.getElementById("#todays-questions"));

    $('a[data-toggle="tab"]').on('shown', function (e) {
        if (e.target.hash == '#todays-questions') {
            todaysQuestionsModel.LoadCategory();
        }
    });
});

I would like to be able to wrap this up in a javascript module that I can reuse for different parts of my application, however I am at a loss as to how to generify this code. Ideally I would like to be able to just make a simple function call to configure all of this behaviour automatically.

I assume for the part where the todaysQuestionsModel.LoadCategory() call is made that this should be a callback function. I also assume there should be some way that I don’t have to specify the id selector, but any attempt I have made so far doesn’t seem to work.

Could someone help me out with this, I am quite out of my depth here :-).

Cheers,

Aidan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:12:14+00:00Added an answer on June 3, 2026 at 1:12 am

    I would use the event handling by knockout.
    This is a modified sample code: http://jsfiddle.net/xVxKD/52/

       <div class="tabbable">
          <ul class="nav nav-tabs" data-bind="foreach: tabs">
            <li data-bind="css: { active: $root.selected() === $data }">
                <a href="#" data-bind="click: $root.doSomething, text: title"></a>
            </li>
          </ul>
          <div class="tab-content" data-bind="foreach: tabs">
            <div class="tab-pane" data-bind="css: { active: $root.selected() === $data }">
              <p data-bind="text: content"></p>
            </div>
          </div>
        </div>​
    
    
    <script type='text/javascript'>//<![CDATA[ 
    $(window).load(function(){
    function Tab(id, title, content) {
        this.id = ko.observable(id);
        this.title = ko.observable(title);
        this.content = ko.observable(content);
    }
    
    var viewModel = {
        tabs: ko.observableArray([
            new Tab(1, "one", "one content"), 
            new Tab(2, "two", "two content"), 
            new Tab(3, "three", "three content")
        ]),
        selected: ko.observable(),        
    
        doSomething: function($data, $event){
            //alert(' = '+$event.target.hash)
            $data.content($data.content() + '\n Do i need to fetch new content for you?')
            viewModel.selected($data);
        }
    
    };
    
    //select second tab by default
    viewModel.selected(viewModel.tabs()[1]);
    
    ko.applyBindings(viewModel);
    });//]]>  
    
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a very specific editor with syntax highlight. I've made all
I'm trying to build a very simple proof-of-concept for the guys I work for
I am currently trying to build a very basic serial shell with my arduino.
I'm trying to write a very lightweight build script which will basically just get
Trying to build a plugin effect that will some what look better than this
There are some build systems that are able to generate platform specific project files
I've been trying to wrap my head around routing and for that very reason
I'm very new to Django and am trying to build my first good app.
Ok, what I'm trying to do is build a web app that allows students
I'm trying to build a very simple location-based app and I have all of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.