-
Notifications
You must be signed in to change notification settings - Fork 19
Day 002 #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: day-002
Are you sure you want to change the base?
Day 002 #3
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,61 @@ | ||
| function onReady () { | ||
| const addTodoForm = document.getElementById( 'addTodoForm' ); | ||
| const todoList = document.getElementById( 'todoList' ); | ||
| const newTodoText = document.getElementById( 'newTodoText' ); | ||
| initUi(); | ||
|
|
||
| // <ul></ul> | ||
| todoList.textContent = ''; | ||
| // it's a little confusing to see you do it without a state object | ||
| // because in the first week's activities, the todo array is nested inside | ||
| // state{}. Not a big deal, but might throw some people off. | ||
| let todos = []; | ||
|
|
||
| addTodoForm.addEventListener( 'submit', event => { | ||
| event.preventDefault(); | ||
| const title = newTodoText.value; | ||
| function createNewTodo ( title ) { | ||
| const newTodo = {title, complete: false} | ||
|
|
||
| // add it to the list | ||
| // create an li | ||
| // <li>{title}</li> | ||
| const newLi = document.createElement( 'li' ); | ||
| newLi.textContent = title; // vs newLi.innerHTML | ||
| todos.push(newTodo) | ||
|
|
||
| newLi.addEventListener( 'click', () => { | ||
| newLi.classList.toggle( 'todo--complete' ); | ||
| }); | ||
| // The "state" changed, so re-draw the UI | ||
| renderTheUi( newTodo ) | ||
| // I see where you're going here, esp. towards the React lifecycles. | ||
| // It's useful to see it built from scratch. | ||
| // Is this something that developed over time and React just formalized, | ||
| // or is this a pattern React is championing and is just becoming defacto? | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More the latter; the idea is to make the UI a function of state ( |
||
| } | ||
|
|
||
| // put it in the ul | ||
| todoList.appendChild( newLi ); | ||
| function renderTheUi ({title, complete}) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem to be the same function signature as the activities.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I was confused about how to solve this one. I ventured away from the signature. Now I know when you write a signature in the Activities you intend it to stay that way! Thanks.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lol Yeah, but there's a specific reason for this one. How might you handle the case where you need to load an initial set of todos (e.g. from the server)?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. word. i think i solved for this in 2nd PR I made on 'day-001' but I may be missing the point altogether. |
||
| // although this works, it may not be the smartest solve. | ||
| // it works if the only reason to render the UI is a new <li> being added, | ||
| // which will likely not be the case as the app grows. See comments below for more. | ||
|
|
||
| newTodoText.value = ''; | ||
| }); | ||
| const newLi = document.createElement( 'li' ); | ||
| newLi.textContent = title; // vs newLi.innerHTML | ||
|
|
||
| newLi.addEventListener( 'click', () => { | ||
| newLi.classList.toggle( 'todo--complete' ); | ||
| }); | ||
|
|
||
| todoList.appendChild( newLi ); | ||
| } | ||
|
|
||
| function initUi() { | ||
| const addTodoForm = document.getElementById( 'addTodoForm' ); | ||
| const todoList = document.getElementById( 'todoList' ); | ||
| const newTodoText = document.getElementById( 'newTodoText' ); | ||
| const filterButton = document.getElementById('toggleBtn') | ||
|
|
||
| // <ul></ul> | ||
| todoList.textContent = ''; | ||
|
|
||
| addTodoForm.addEventListener( 'submit', event => { | ||
| event.preventDefault(); | ||
| createNewTodo( newTodoText.value ); | ||
| newTodoText.value = ''; | ||
| }); | ||
|
|
||
| // you mentioned doing this DOM query stuff was inefficient b/c the DOM is slow. | ||
| // lookng forward to see how else we could do this. | ||
| filterButton.addEventListener('click', event => { | ||
| const items = document.querySelectorAll('.todo--complete'); | ||
| items.forEach( item => item.classList.toggle('hide') ); | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| if ( document.readyState !== 'loading' ) { | ||
|
|
@@ -33,3 +64,26 @@ if ( document.readyState !== 'loading' ) { | |
| document.addEventListener( 'DOMContentLoaded', onReady ); | ||
| } | ||
|
|
||
| /* | ||
| REFLECTION | ||
|
|
||
| Challenges: | ||
| * Knowing what is already in the DOM and what is being added. in renderTheUI(), my instinct | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are all good thoughts. One feedback I would provide is to avoid premature optimization. Where do you think a framework fits in here? How do they solve some of these challenges?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the framework would fit in when you need a direct and efficient relationship between data and the interface. Simply put, if there will be dynamic data, I want to instantly jump into React. Is that a crutch? Maybe?? But I don't see it being such a bad one if the tradeoff of relatively little bloat for ease of use is what i think it is. True?
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is definitely a degree of "developer ergonomics" here, but I think there's more to it than that. Are you familiar with the virtual DOM in react? (ng2 has a similar concept) |
||
| is to clear the list, and loop through the todos array to make a new one. But something | ||
| tells me this is inefficient and not the best strategy. | ||
|
|
||
| > I see two choices. One, clear the array and refill it every time, or only update the UI | ||
| with new data received. I think the latter, though more efficient, may leave room for error | ||
| if updates are made from other parts of the codebase...? | ||
|
|
||
| Ways to Make It Better: | ||
| * not sure yet | ||
|
|
||
| Generalizations: | ||
| * Like React, knowing what needs to be updated in the DOM and what will be the same as in the last | ||
| render would be nice, but I guess that's why we have React, right?? | ||
|
|
||
| COMMENTS | ||
| Part 2 of the Activities was way beyond what I could tackle on my own in just three days. Does this | ||
| mean I have gaping knowledge gap and I will be behind the curve next class? | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Emphatically not. 😄
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. whew. thanks. |
||
| */ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,9 @@ | |
| .todo--complete { | ||
| text-decoration: line-through; | ||
| } | ||
| .hide { | ||
| display: none; | ||
| } | ||
| </style> | ||
| </head> | ||
| <body> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting feedback. Frankly, I never considered the activities related.