Просмотр исходного кода

Update recipe book to use a template

Lee Morgan 6 лет назад
Родитель
Сommit
517ee34ab4
3 измененных файлов с 16 добавлено и 14 удалено
  1. 1 1
      views/dashboardPage/dashboard.css
  2. 7 0
      views/dashboardPage/dashboard.ejs
  3. 8 13
      views/dashboardPage/recipeBook.js

+ 1 - 1
views/dashboardPage/dashboard.css

@@ -1,5 +1,5 @@
 /* 
 /* 
-General page styling 
+General page styling
 */
 */
 html{
 html{
     height: 100%;
     height: 100%;

+ 7 - 0
views/dashboardPage/dashboard.ejs

@@ -165,6 +165,13 @@
                 </div>
                 </div>
 
 
                 <div id="recipeList"></div>
                 <div id="recipeList"></div>
+
+                <template id="recipe">
+                    <div class="itemDisplay">
+                        <p></p>
+                        <p></p>
+                    </div>
+                </template>
             </div>
             </div>
 
 
             <div id="ordersStrand" class="strand">
             <div id="ordersStrand" class="strand">

+ 8 - 13
views/dashboardPage/recipeBook.js

@@ -11,27 +11,22 @@ window.recipeBookStrandObj = {
     },
     },
 
 
     populateRecipes: function(){
     populateRecipes: function(){
-        let recipeList = document.querySelector("#recipeList");
+        let recipeList = document.getElementById("recipeList");
+        let template = document.getElementById("recipe").content.children[0];
 
 
         this.recipeDivList = [];
         this.recipeDivList = [];
         while(recipeList.children.length > 0){
         while(recipeList.children.length > 0){
             recipeList.removeChild(recipeList.firstChild);
             recipeList.removeChild(recipeList.firstChild);
         }
         }
 
 
-        for(let recipe of merchant.recipes){
-            let recipeDiv = document.createElement("div");
-            recipeDiv.classList = "itemDisplay";
-            recipeDiv.onclick = ()=>{recipeDetailsComp.display(recipe)};
-            recipeDiv._name = recipe.name;
+        for(let i = 0; i < merchant.recipes.length; i++){
+            let recipeDiv = template.cloneNode(true);
+            recipeDiv.onclick = ()=>{recipeDetailsComp.display(merchant.recipes[i])};
+            recipeDiv._name = merchant.recipes[i].name;
             recipeList.appendChild(recipeDiv);
             recipeList.appendChild(recipeDiv);
 
 
-            let recipeName = document.createElement("p");
-            recipeName.innerText = recipe.name;
-            recipeDiv.appendChild(recipeName);
-
-            let recipePrice = document.createElement("p");
-            recipePrice.innerText = `$${(recipe.price / 100).toFixed(2)}`;
-            recipeDiv.appendChild(recipePrice);
+            recipeDiv.children[0].innerText = merchant.recipes[i].name;
+            recipeDiv.children[1].innerText = `$${(merchant.recipes[i].price / 100).toFixed(2)}`;
 
 
             this.recipeDivList.push(recipeDiv);
             this.recipeDivList.push(recipeDiv);
         }
         }