|
| 1 | +import * as model from './model.js'; |
| 2 | +import recipeView from './views/recipeView.js'; |
| 3 | +import searchView from './views/searchView.js'; |
| 4 | +import resultsView from './views/resultsView.js'; |
| 5 | +import paginationView from './views/paginationView.js'; |
| 6 | + |
| 7 | +// to make sure our app is supported by old browsers: |
| 8 | +import 'core-js/stable'; // polyfill everything else |
| 9 | +import 'regenerator-runtime/runtime'; //polyfill async await |
| 10 | +// import recipeView from './views/recipeView.js'; |
| 11 | + |
| 12 | +// if (module.hot) { |
| 13 | +// module.hot.accept(); |
| 14 | +// } |
| 15 | + |
| 16 | +const controlRecipes = async function () { |
| 17 | + try { |
| 18 | + // id |
| 19 | + const id = window.location.hash.slice(); |
| 20 | + |
| 21 | + // guard clause |
| 22 | + if (!id) return; |
| 23 | + // listen for the event of the entire page loading |
| 24 | + // render spinner |
| 25 | + recipeView.renderSpinner(); //you can do this with all other views |
| 26 | + |
| 27 | + // load recipe |
| 28 | + await model.loadRecipe(id); //1. we receive data here |
| 29 | + // loadRecipe is an async function and will return a promise; we thus have to await that promise before we can move on to execution |
| 30 | + // this is an example of one async function calling another async function |
| 31 | + // const recipe = model.state.recipe |
| 32 | + |
| 33 | + // console.log(recipe); |
| 34 | + // 2) Rendering recipe |
| 35 | + recipeView.render(model.state.recipe); // 2. we render data here |
| 36 | + // 'render' is a very common name for render e.g., in React |
| 37 | + // const recipeView = new recipeView(model.state.recipe)// also possible |
| 38 | + // render method will accept the data from recipe and store it in recipeView object |
| 39 | + |
| 40 | + // TEST |
| 41 | + controlServings() |
| 42 | + } catch (err) { |
| 43 | + recipeView.renderError(); |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | +const controlSearchResulsts = async () => { |
| 48 | + try { |
| 49 | + resultsView.renderSpinner(); |
| 50 | + console.log(resultsView); |
| 51 | + // 1) Get search query |
| 52 | + const query = searchView.getQuery(); |
| 53 | + if (!query) return; |
| 54 | + |
| 55 | + // 2) Load search results |
| 56 | + await model.loadSearchResults(query); |
| 57 | + |
| 58 | + // 3) Render results |
| 59 | + // resultsView.render(model.state.search.results) |
| 60 | + resultsView.render(model.getSearchResultsPage()); |
| 61 | + |
| 62 | + // 4) Render initial pagination buttons |
| 63 | + paginationView.render(model.state.search); // pass in the whole object |
| 64 | + // console.log(model.getSearchResultsPage(1)); |
| 65 | + } catch (err) { |
| 66 | + console.error(err); |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | +// Publisher-subscriber pattern in practice |
| 71 | +// init function is called when the program starts which immediately calls the addHandlerRender function from the view |
| 72 | +// this is possible because the controller does in fact import both the view and the model |
| 73 | +// as we call addHandlerRender, we pass in controlRecipes as an argument |
| 74 | +// essentially, we subscribe controlRecipes to addHandlerRender |
| 75 | +// at this point, the two functions are essentially finally connected |
| 76 | +// addHandlerRender thus listens for events (addEventListener), and uses controlRecipes as a callback |
| 77 | +// as soon as the publisher publishes an event, the subscriber will get called |
| 78 | +// this will allow keeping the handler in the controller and the listener in the view |
| 79 | + |
| 80 | +// analyze controlSearchResulsts and controlPagination; they're quite similar and inter-twined |
| 81 | + |
| 82 | +const controlPagination = goToPage => { |
| 83 | + // 1) Render new results |
| 84 | + // resultsView.render(model.state.search.results) |
| 85 | + resultsView.render(model.getSearchResultsPage(goToPage)); |
| 86 | + //this works because render will overwrite the markup that was there previously; because of the clear() method |
| 87 | + |
| 88 | + // 2) Render new pagination buttons |
| 89 | + paginationView.render(model.state.search); |
| 90 | +}; |
| 91 | + |
| 92 | +const controlServings = () => { |
| 93 | + // update the recipe servings (in state) |
| 94 | + model.updateServings(8) |
| 95 | + // update the recipe view; we'll simply re-render the recipe instead of manually changing the quantitity elements |
| 96 | + recipeView.render(model.state.recipe); |
| 97 | +} |
| 98 | + |
| 99 | +const init = () => { |
| 100 | + recipeView.addHandlerRender(controlRecipes); |
| 101 | + |
| 102 | + // the event is listened for in addHandlerRender but handled here |
| 103 | + searchView.addHandlerSearch(controlSearchResulsts); |
| 104 | + paginationView.addHandlerclick(controlPagination); |
| 105 | +}; |
| 106 | +init(); |
0 commit comments