Click here to Skip to main content
12,755,516 members (38,213 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 8 Mar 2014

Integrating Knockout with Twitter Bootstrap Popover

, 8 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Integrating Knockout with Twitter Bootstrap Popover


Knockout is a powerful and pretty useful javascript library to have the 2 way binding MVVM (Model-View-ViewModel) pattern implementation along with the templates and built-in binding support to
bind the data to the HTML controls.<o:p />

Knockout also does support the developers to write the custom binding (out of the box) and can use those binding to bind the data to the HTML controls.<o:p />

Twitter Bootstrap is a web UI toolkit (HTML / CSS and Javascript) to build the rich and responsive web application.

To know more about the Twitter Bootstrap Popover click here

The below code explain how to bind the model (data) using Knockout to the Twitter Bootstrap Popover.

For this code snippet, I have used Knockout Custom Binding Handler. To know more about the Knockout Customer Binding Handler click here

<o:p />

Look & feel

1. The initial data displayed in the table.

2. After clicking the "Edit", the popover with the corresponding person's data will be displayed.

3. On changing any data in the popover, the corresponding data will be updated in the view.

Using the code

Below is the working code for binding the knockout view model with the twitter bootstrap popover.

In the initial of the code, I have given the references to the CDN path for the Knockout, JQuery and Bootstrap.

Next is, I have created a Person ViewModel which is a collections of Persons (Observable Array) that will be bind to the Popover (View)

Now the key part is to define the Knockout Custom Binding Handler. The KO custom binding handler will have two methods i.e. init and update. Here I have set the properties in the init method like the element which binds this handler and passes the "id", the dynamic template content. Using bindingContext, we can attach the ViewModel to the Popover HTML content.

Also to make sure, the popover trigger property should be set to "manual" and binding will be handled in the click event.

Using the Knockout template ("person-template") and the View Model, the template will be rendered with the support of 2 way binding. Any changes made to any of the data in the popover will be reflected back to the view i.e. the Person Table.

<!doctype html>
<html lang="en">
    <meta charset="utf-8">
    <title>Knockout and Bootstrap Popover</title>
    <link href="" rel="stylesheet">
    <link href="" rel="stylesheet">
    <script src=""></script>
    <script src=""></script>
    <script src=""></script>
        var personViewModel = function () {
            var self = this;
            self.persons = ko.observableArray();
                id: 1,
                firstName: ko.observable("John"),
                lastName: ko.observable("Doe"),
                email: ko.observable("")
                id: 2,
                firstName: ko.observable("Greg"),
                lastName: ko.observable("Smith"),
                email: ko.observable("")
                id: 3,
                firstName: ko.observable("Tom"),
                lastName: ko.observable("Walter"),
                email: ko.observable("")

            self.closePopover = function (person) {
                $('#popover' + + "_click").popover("hide");



        ko.bindingHandlers.popUp = {
            init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
                var attribute = ko.utils.unwrapObservable(valueAccessor());
                var templateContent = attribute.content;
                var popOverTemplate = "<div class='popOverClass' id='" + + "-popover'>" + $(templateContent).html() + "</div>";
                    placement: 'right',
                    content: popOverTemplate,
                    html: true,
                    trigger: 'manual'
                $(element).attr('id', "popover" + + "_click");
                $(element).click(function () {
                    var thePopover = document.getElementById( + "-popover");
                    childBindingContext = bindingContext.createChildContext(viewModel);
                    ko.applyBindingsToDescendants(childBindingContext, thePopover);

    <br />
    <br /><br /><br /><br /><br /><br />
    <table border="1" cellpadding="0" cellspacing="0" data-bind="foreach: persons">
            <td><span data-bind="text: firstName"></span></td>
            <td><span data-bind="text: lastName"></span></td>
            <td><span data-bind="text: email"></span></td>
            <td><a href="#" data-bind="popUp: {content: '#person-template', id: id}">Edit</a></td>
    <script type="text/html" id="person-template">
        <table border="1" cellpadding="0" cellspacing="0">
                <td>First Name</td>
                <td><input type="text" data-bind="value: firstName, valueUpdate:'keyup'" /></td>
                <td>Last Name</td>
                <td><input type="text" data-bind="value: lastName, valueUpdate:'keyup'" /></td>
                <td>Email Address</td>
                <td><input type="text" data-bind="value: email, valueUpdate:'keyup'" /></td>
        <div class="row-fluid">
                <div class="span3 offset1">
                    <button class="btn btn-small btn-danger" data-bind="click:$root.closePopover">Close</button>
        ko.applyBindings(new personViewModel());


I hope it will helpful to the developers who are building the rich and responsive UI using knockout and Twitter bootstrap. Though I have used the pre-populated data but you can get data from any of the services.

Thanks for reading.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

No Biography provided

You may also be interested in...


Comments and Discussions

QuestionAnother version: KO.Validation with Twitter Bootstrap Popover Pin
Dark_V7-May-14 10:29
memberDark_V7-May-14 10:29 
SuggestionNice Stuff But Pin
Saqib Shakil27-Apr-14 20:28
memberSaqib Shakil27-Apr-14 20:28 
Questionis that so..... Pin
Richard C. Lanasa9-Mar-14 18:24
memberRichard C. Lanasa9-Mar-14 18:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170217.1 | Last Updated 9 Mar 2014
Article Copyright 2014 by Mantri Vishal
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid