Learning Vue.js - A Task List

in #javascript6 years ago

Getting Into the Programming Mindset

It's been difficult for me to get back into the programming mindset. I had to just go somewhere out of my normal environment and force myself to go through the tutorial that I'm working through. I'm glad I'm pushing through it, because once I got to programming and solving problems, I liked it and got satisfaction from figuring things out.

LearnVue2-episode6.jpg

Episode 6

Here's the learning resource that I'm currently working through. The coding challenge I did was at the last part of Episode 6:

https://laracasts.com/series/learn-vue-2-step-by-step/episodes/6

I imagined an approach and then worked to figure out how to do it step by step. The example was to have three lists of tasks:

  • All Tasks
  • Incomplete Tasks
  • Complete Tasks

The instructor in the video showed how to use computed properties within view to figure out what should be in the "Incomplete Tasks" and "Complete Tasks" lists before displaying them on the page.

Then he had a challenge:

Make it so a button can change if a task is completed or not.

I had an idea of how to do this, but it took me a while to figure out. I couldn't remember how to pass the object I wanted to change to the function in the Vue method. I eventually figured it out with:

<button v-on:click="toggle(task)">Toggle</button>

When the button is clicked, it sends the "task" object to the "toggle" method. Here's the code for the "toggle" method I wrote:

toggle: function (task) {
  if (task.completed === false ) {
      task.completed = true
    } else {
      task.completed = false
    }
  }

I had to figure out that I needed to send the "task" object to the function as an argument (parameter?) and then change whether it was completed or not with an if statement.

See my code on jsFiddle

jsFiddle is a great way to share code and the result. Here's what I ended up with:

https://jsfiddle.net/2hmu4yyh/

If you go there and press the "toggle" button on the All Tasks list, it will change if that task is completed or not and then that is reactively updated in the computed lists for "Incomplete Tasks" and "Complete Tasks".

I also figured out how to make the completed tasks use a different style, making the text green and bold. I accomplished this with the v-bind directive to call a class if the "completed" was true for that task.

After I did all this, which was a good exercise, I found in the comments that some people had shared their solutions on jsFiddle, too. The one I really liked used checkboxes and found a way to use the true or false value of the checkbox to connect with the true or false of the "completed" value in the task object using v-model. That was really good! I had thought about using checkboxes, but wasn't sure how and I definitely wouldn't have thought of the elegant solution that they came up with. Here's that jsFiddle solution that I liked:

https://jsfiddle.net/ug69y13s/1/

Then someone else took that idea and made it look great with some styles:

https://jsfiddle.net/razvantirboaca/9aqsjj30/

I Can Read Code!

One of the great things I realized is that in viewing other people's solutions, I was mostly understanding their code. That's one of my goals is to be able to be familiar enough with this so that I can read other people's code and understand what's going on.

I'm making progress.

Thanks for reading!

-- @matthewdavid

Sort:  

Great work! Here's another way to eliminate the method but keep the button and click binding:

<button @click="task.completed = !task.completed">Toggle</button>

Here's the fiddle:

https://jsfiddle.net/harps116/2hmu4yyh/2/

This technique is pretty handy for toggle scenarios. Of course you could abstract the logic into a method for reuse and have a global toggle utility method which might be useful.

Thank you. I think that this "demo app" idea of a Task List or Todo List is a good way to learn. I think I will create one for myself with a database backend as a way to learn how to put everything together.

It’s great following your programming journey @matthewdavid - thanks for the link to laracasts. I’d never come across this site before. Will have to check it out in more detail.

Keep posting on how you’re doing with the programming career.

Indeed this was really interesting
I believe with hardworking you'll become a very great computer programmer

Thank you. I appreciate that. I've wanted to be a programmer since I was young. Looks like I'm about to finally make that dream come true.

Awwn ☺✌
That will be fulfilling

Hey Matthew, thanks for your tutorial. Have you worked with React as well?

I've read quite a bit about React and done a few intro tutorials, but not as much as I've done with Vue.

I was quite surprised that Vue.js has now more stars on Github than React. Will come back to your tutorials when I learn Vue.js. Cheers.

Coin Marketplace

STEEM 0.27
TRX 0.13
JST 0.032
BTC 62767.81
ETH 2942.54
USDT 1.00
SBD 3.55