Преглед изворни кода

Refactor recipeBookObj display

Lee Morgan пре 6 година
родитељ
комит
c32e6652d3
1 измењених фајлова са 32 додато и 28 уклоњено
  1. 32 28
      views/dashboardPage/recipeBook.js

+ 32 - 28
views/dashboardPage/recipeBook.js

@@ -3,39 +3,28 @@ window.recipeBookStrandObj = {
 
     display: function(){
         if(!this.isPopulated){
-            let recipeList = document.querySelector("#recipeList");
+            this.populateRecipes();
 
-            for(let recipe of merchant.recipes){
-                let recipeDiv = document.createElement("div");
-                recipeDiv.classList = "recipeItem";
-                recipeDiv.onclick = ()=>{this.displayRecipe(recipe)};
-                recipeList.appendChild(recipeDiv);
+            this.isPopulated = true;
+        }
+    },
 
-                let recipeName = document.createElement("p");
-                recipeName.innerText = recipe.name;
-                recipeDiv.appendChild(recipeName);
+    populateRecipes: function(){
+        let recipeList = document.querySelector("#recipeList");
 
-                let recipePrice = document.createElement("p");
-                recipePrice.innerText = `$${(recipe.price / 100).toFixed(2)}`;
-                recipeDiv.appendChild(recipePrice);
-            }
+        for(let recipe of merchant.recipes){
+            let recipeDiv = document.createElement("div");
+            recipeDiv.classList = "recipeItem";
+            recipeDiv.onclick = ()=>{this.displayRecipe(recipe)};
+            recipeList.appendChild(recipeDiv);
 
-            let ingredientsSelect = document.querySelector("#recipeInputIngredients select");
-            let categories = categorizeIngredients();
-            for(let category of categories){
-                let optgroup = document.createElement("optgroup");
-                optgroup.label = category.name;
-                ingredientsSelect.appendChild(optgroup);
-
-                for(let ingredient of category.ingredients){
-                    let option = document.createElement("option");
-                    option.value = ingredient.id;
-                    option.innerText = ingredient.name;
-                    optgroup.appendChild(option);
-                }
-            }
+            let recipeName = document.createElement("p");
+            recipeName.innerText = recipe.name;
+            recipeDiv.appendChild(recipeName);
 
-            this.isPopulated = true;
+            let recipePrice = document.createElement("p");
+            recipePrice.innerText = `$${(recipe.price / 100).toFixed(2)}`;
+            recipeDiv.appendChild(recipePrice);
         }
     },
 
@@ -68,6 +57,21 @@ window.recipeBookStrandObj = {
 
     displayAddRecipe: function(){
         openSidebar(document.querySelector("#addRecipe"));
+
+        let ingredientsSelect = document.querySelector("#recipeInputIngredients select");
+        let categories = categorizeIngredients();
+        for(let category of categories){
+            let optgroup = document.createElement("optgroup");
+            optgroup.label = category.name;
+            ingredientsSelect.appendChild(optgroup);
+
+            for(let ingredient of category.ingredients){
+                let option = document.createElement("option");
+                option.value = ingredient.id;
+                option.innerText = ingredient.name;
+                optgroup.appendChild(option);
+            }
+        }
     },
 
     changeRecipeCount: function(){