knockout.JS - using bootstrap modal popup

801 views
Skip to first unread message

ay1585

unread,
Apr 20, 2014, 12:26:44 PM4/20/14
to knock...@googlegroups.com

Hello All,

I am very new to KO.JS.

I am facing an issue in displaying data in modal popup. 

Clicking on anchor tag should open modal popup

<a data-target="#news0" data-bind="attr: { 'data-target': '#news' + $index() }" data-toggle="modal" href="#">
Read Full Notes
</a>

I created a template and am using template binding to display modal popup.

<script type="text/html" id="tmplModal">
<div data-bind="attr: { 'id': '#news' + $index() }" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel" data-bind="text: Title"></h3>
</div>
<div class="modal-body" data-bind="text: Desc">
</div>
</div>
</script>


In Firefox debugger, we are able to get the data for popup - title and desc also. here's the code which is generated in run-time with the above template:

Is it not sufficient to give data-toggle and data-bind attributes on an anchor tag - to display a bootstrap  modal pop up?

I have the references to bootstrap.min.js and its css at the top of the page.

If I dont use KO.JS, this same code works very much fine in straight forward html. 

Please suggest.

Its been nearly a week am unable to solve this. I badly need help. 

Thanks. 

Patrick Steele

unread,
Apr 21, 2014, 7:47:18 AM4/21/14
to knock...@googlegroups.com
Are you having problems showing the modal popup using knockout?  Or are you having problems display data inside the modal using knockout?

--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Lars GBN

unread,
Apr 21, 2014, 6:36:48 PM4/21/14
to knock...@googlegroups.com
I'm not sure what the purpose of this part of your application is about, but try this out.

<div data-bind="foreach: articles">
    <span data-bind="text: Title"></span>
    <a href="#"
        data-bind="click: function() { $root.openModal($index()); }">
        Read Full Notes
    </a>
</div>

<aside class="modal fade">
    <div class="modal-dialog">
        <article class="modal-content"
            data-bind="with: modalArticle>
            <section class="modal-header">
                <button class="close" type="button" aria-hidden="true"
                    data-bind="closeModal">
                    &times;
                </button>

                <h3 class="modal-title"
                    data-bind="text: Title">
                </h3>
            </section>
            <section class="modal-body">
                <p data-bind="text: Desc"></p>
            </section>
            <section class="modal-footer">
                <button class="btn btn-default" type="button"
                    data-bind="click: function() { $root.closeModal(); }">
                    Close
                </button>
            </section>
        </article>
    </div>
</aside>

(function() {
    'use strict';
    var ModalViewModel, NewsArticleViewModel, articlesCollection;

    // Get from API, etc.
    articlesCollection = [
        {
            Title: 'Aenean ornare leo eleifend erat',
            Desc: 'Etiam blandit est at ante aliquam, dapibus ornare est egestas. Suspendisse potenti. Praesent scelerisque tristique nulla, nec mollis eros vulputate vitae. Nam ac lacus ut risus bibendum ultricies vel eu dui. Nulla odio nibh, varius id pretium sit amet, tincidunt dapibus est. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec erat leo, ornare ac adipiscing vitae, lobortis a arcu'
        }, {
            Title: 'Donec egestas, dui sed faucibus',
            Desc: 'Etiam tincidunt facilisis lobortis. Proin fermentum vulputate ipsum. Praesent est nisl, molestie eu leo vel, ullamcorper rutrum felis. Ut tempus turpis ligula, eget sollicitudin nisi adipiscing in. In semper facilisis feugiat. Nullam placerat pulvinar velit et aliquet. Curabitur sapien odio, vestibulum a luctus ac, rhoncus nec ante. Nam blandit interdum mauris quis cursus. Phasellus sagittis ipsum et est imperdiet, ac porta neque porta'
        }, {
            Title: 'In hendrerit, eros non feugiat',
            Desc: 'Proin dictum eros ac pulvinar bibendum. Duis dolor lectus, malesuada non accumsan non, hendrerit vitae arcu. Morbi vel felis tincidunt, convallis massa eleifend, blandit ligula. Sed non vestibulum diam, et molestie nisl. Nam id aliquam eros. Sed condimentum sem at diam consequat, quis molestie neque luctus. Aenean lectus tortor, vehicula sit amet iaculis quis, blandit eu odio. Vivamus et interdum diam. Etiam a tortor in elit scelerisque sollicitudin. Pellentesque quis aliquet ipsum. Maecenas a est in magna luctus dignissim'
        }
    ];

    NewsArticleViewModel = (function() {
        function NewsArticleViewModel(Title, Desc) {
            if (Title == null) {
                Title = '';
            }

            if (Desc == null) {
                Desc = '';
            }

            this.Title = ko.observable(Title);
            this.Desc = ko.observable(Desc);
        }

        return NewsArticleViewModel;

    })();

    ModalViewModel = (function() {
        function ModalViewModel(articles) {
            var article, i, articlesCount;

            this.articles = []

            for (i = 0, articlesCount = articles.length; i < articlesCount; i++) {
                article = articles[i];

                this.articles.push(new NewsArticleViewModel(article.Title, article.Desc));
            }

            this.modalArticle = new NewsArticleViewModel();
        }

        ModalViewModel.prototype.closeModal = function() {
            modalArticle.Title('');
            modalArticle.Desc('');

            $('#newsModal').modal('close');
        };

        ModalViewModel.prototype.openModal = function(articleIndex) {
            var article;

            article = this.articles()[articleIndex];

            this.modalArticle.Title(article.Title);
            this.modalArticle.Desc(article.Desc);

            $('#newsModal').modal('open');
        };

        return ModalViewModel;
    })();

    ko.applyBindings(new ModalViewModel(articlesCollection));
})();
Reply all
Reply to author
Forward
0 new messages