Bläddra i källkod

Second course example: 2.3.5.zip.

Frederic G. MARAND 8 år sedan
förälder
incheckning
9574f34a94
1 ändrade filer med 90 tillägg och 36 borttagningar
  1. 90 36
      index.html

+ 90 - 36
index.html

@@ -1,56 +1,110 @@
-<!-- This is a first example of using a template -->
+<!-- This is an example of using the same template with
+  different data -->
 
 <head>
-  <!-- include our libraries and css files -->
-  <script src="js/jquery-2.1.4.min.js"></script>
-  <script src="js/handlebars-v3.0.3.js"></script>
 
-  <script src="js/bootstrap.min.js"></script>
+<!-- include our libraries and css files -->
 
-  <link href="css/bootstrap.css" rel="stylesheet">
-  <link href="css/gallery.css" rel="stylesheet">
+<script src="js/jquery-2.1.4.min.js"></script>
+<script src="js/handlebars-v3.0.3.js"></script>
+
+<script src="js/bootstrap.min.js"></script>
+
+<link href="css/bootstrap.css" rel="stylesheet">
+<link href="css/gallery.css" rel="stylesheet">
 
 </head>
 
-<body>
-  <!-- the content of the web page starts off empty
-   because we will fill it later from the template -->
-  <div id="content">
-  </div>
+<body role="document">
 
-  <!-- this is our template
-    it displays an image with a title and author headings
-    the bits in curly brackets {{}} are template expressions -->
-  <script id="image-template" type="text/x-handlebars-template">
-    <div class="title">
-      <h1>{{title}}</h1>
-      <h3 class="author">
-        {{author}}
-      </h3>
-      <img style="height:600" src="{{src}}" />
+  <div class="container">
+
+    <div class="page-header">
+      <h1>My photo albums </h1>
     </div>
-  </script>
+
+    <!-- this is a simple nav bar for selecting between images -->
+    <ul class="nav nav-tabs">
+          <li role=""  id="image1btn">
+            <a href="#">Image 1</a>
+            </li>
+          <li role="" id="image2btn">
+            <a href="#">Image 2</a>
+            </li>
+        </ul>
+    <br/><br/>
+
+
+
+    <!-- the content of the web page starts off empty
+     because we will fill it later from the template -->
+    <div id="content"  class="container-fluid"  role="main">
+    </div>
+
+  </div>
+
+    <!-- this is our template
+      it displays an image with a title and author headings
+      the bits in curly brackets {{}} are template expressions.
+      This code uses Bootstrap to provide a grid based layout
+      -->
+    <script id="detail-template" type="text/x-handlebars-template">
+      <br>
+      <div class="row-fluid">
+        <div class="col-sm-5">
+
+          <img  style="width:100%" src="{{src}}"/>
+        </div>
+  		  <div class="col-sm-7">
+          <h1>{{title}}</h1>
+  		    <h3 class="author">
+  		      {{author}}
+  		    </h3>
+        </div>
+
+        </div>
+      </div>
+    </script>
 
   <!-- javascript code to fill the template -->
   <script type="text/javascript">
-    // grab our template code from the DOM
-    var source = $("#image-template").html();
 
-    // compile the template so we can use it
-    var template = Handlebars.compile(source);
 
-    // create some data
-    var data = {
-      src: "images/The_Earth_seen_from_Apollo_17.jpg",
-      title: "The Earth seen from Apollo 17",
-      author: "Ed g2s"
+    // have two objects each representing a different image
+    var data1 = {
+      src: "https://upload.wikimedia.org/wikipedia/commons/thumb/9/97/The_Earth_seen_from_Apollo_17.jpg/600px-The_Earth_seen_from_Apollo_17.jpg", 
+      title:"The Earth seen from Apollo 17",
+      author:"Ed g2s"
     };
 
-    // generate HTML from the data
-    var html = template(data);
+    var data2 = {
+        src: "https://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Shopping_Center_Magna_Plaza_Amsterdam_2014.jpg/600px-Shopping_Center_Magna_Plaza_Amsterdam_2014.jpg",
+        title: "Shopping Center Magna Plaza Amsterdam 2014",
+        author: "Tuxyso",
+    };
+
+    // get the template and compile it once when
+    // the page loads
+    var source   = $("#detail-template").html();
+    var detail_template = Handlebars.compile(source);
+
+    // instantiate the tempalte with data whenever you click
+    // there are two separate functions, one for each
+    // image (next week we will see a better way of doing this)
+    $("#image1btn").click(function () {
+      var html    = detail_template(data1);
+
+      $('#content').html(html);
+    });
+
+
+    $("#image2btn").click(function () {
+      var html    = detail_template(data2);
+
+      $('#content').html(html);
+    });
+
 
-    // add the HTML to the content div
-    $('#content').html(html);
   </script>
 
 </body>