Advertisement
dariuszrorat

Bootstrap KnockoutJS random cats gallery

Dec 13th, 2017
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.42 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="pl">
  3.  
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <title>Cats gallery</title>
  7.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  8.     <style>
  9.     .modal-body {text-align: center; }
  10.     .modal-body img {max-width: 100%;}
  11.     </style>
  12. </head>
  13.  
  14. <body>
  15.  
  16.     <div class="container">
  17.         <div class="jumbotron">
  18.             <h1>Cats gallery</h1>
  19.             <p>Bootstrap and KnockoutJS based gallery of cats based on <a href="http://thecatapi.com">http://thecatapi.com</a></p>
  20.         </div>
  21.         <div class="row flex" data-bind="foreach: listItems">
  22.             <div class="col-lg-4 col-sm-6">
  23.                 <a data-bind="attr: {href: '#' + $data.id}" class="thumbnail" data-toggle="modal">
  24.                     <img data-bind="attr: {src: $data.url}" alt="Random cat">
  25.                 </a>
  26.                 <div class="modal" data-bind="attr: {id: $data.id}">
  27.                     <div class="modal-dialog modal-lg">
  28.                         <div class="modal-content">
  29.                             <div class="modal-body">
  30.                                 <img data-bind="attr: {src: $data.url}" alt="Random cat">
  31.                             </div>
  32.                         </div>
  33.                     </div>
  34.                 </div>
  35.             </div>
  36.         </div>
  37.     </div>
  38.  
  39.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  40.     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  41.     <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.0/knockout-min.js"></script>
  42.     <script src="https://cdnjs.cloudflare.com/ajax/libs/x2js/1.2.0/xml2json.js"></script>
  43.  
  44.     <script>
  45.         var ViewModel = function()
  46.         {
  47.             var self = this;            
  48.             self.listItems = ko.observableArray();
  49.  
  50.             self.loadMore = function()
  51.             {
  52.                 self.ajax("http://thecatapi.com/api/images/get?format=xml&results_per_page=4", "GET", {})
  53.                    .done(function(data) {
  54.                        var x2js = new X2JS();
  55.                         var response = x2js.xml2json(data);
  56.                         var images = response.response.data.images.image;
  57.                         for(var i = 0; i < images.length; i++)
  58.                        {
  59.                            self.listItems.push({
  60.                                "id": images[i].id,
  61.                                "url": images[i].url
  62.                            });
  63.                        }
  64.  
  65.                    })
  66.                    .fail(function() {
  67.                        alert('Somethings went wrong!');
  68.                    })
  69.            }
  70.  
  71.            self.ajax = function (url, method, data)
  72.            {
  73.                var request = {
  74.                    url: url,
  75.                    type: method,
  76.                    data: data,
  77.                    dataType: 'xml'
  78.                };
  79.                return $.ajax(request);
  80.            }
  81.  
  82.        }
  83.  
  84.        var viewModel = new ViewModel();
  85.        ko.applyBindings(viewModel);
  86.  
  87.        $(document).ready(function() {
  88.            viewModel.loadMore();
  89.        });
  90.  
  91.        $(window).scroll(function () {
  92.            if ($(window).scrollTop() == $(document).height() - $(window).height()) {
  93.                viewModel.loadMore();
  94.            }
  95.        });
  96.  
  97.    </script>
  98. </body>
  99.  
  100. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement