Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 74 additions & 20 deletions src/index.js
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.
Copy link
Copy Markdown
Owner

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.

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?
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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 (state => ui). A lot of frameworks build in this capability, but really React is the one that takes it all the way. Vue does a little too.

}

// put it in the ul
todoList.appendChild( newLi );
function renderTheUi ({title, complete}) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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)?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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' ) {
Expand All @@ -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
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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?
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Emphatically not. 😄

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whew. thanks.

*/
3 changes: 3 additions & 0 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
.todo--complete {
text-decoration: line-through;
}
.hide {
display: none;
}
</style>
</head>
<body>
Expand Down