This episode is for members only

Sign up to access "Build a Trello Clone With Livewire" right now.

Get started
Already a member? Sign in to continue
Playing
24. Moving archivable things to a trait

Transcript

00:00
We're going to do a couple of things in this episode just to tidy stuff up. We know that we've got the exact same scopes inside of our card and
00:09
our column models because both can be archived. We also have the same casts value in here as well. What we're going to do is move this over to a trait so we can just apply an archivable trait to both the card and column just to keep things tidy.
00:24
Now it's not just a case of just copying and pasting everything over to a trait and using it, we'll have to do a couple of other things as well. What we'll then do is add another scope into our archivable trait which orders the things that we have in our card and column list
00:40
by when they were archived so we have the latest at the top. Okay let's get started on building the trait out so we're going to go over to our models directory and I usually just create our directory in here called traits depending on how big the application is
00:54
and in here we're going to create out a new trait called archivable and of course let's make this a trait and create that out. Okay great so let's just take everything from our card model that
01:11
looks like it should belong in here which is all of this stuff here and let's use this archivable trait in here and we'll open this up and just paste everything in. Okay so there's a couple of reasons why this isn't going to work by default.
01:26
The first one is that we can't just override the casts here or any others that we've already created will just be removed. The second thing is that we need to include the column name but obviously if we apply this trait to the columns table or the columns
01:38
model it's not going to have the same table name. So let's deal with the casts first of all. How do we bind in casts when we have this trait applied? Well when we apply a trait to a model we have an initialized
01:54
trait called so whatever the name of the trait is archivable this will be invoked and let's just get rid of that and we should be good. Now what we can do with our casts is we can say this merge casts and then we can just
02:11
pass in an array of any of the casts that we want to merge in what exists within the base model. So we can just put that in there and that is a job done. Next up for each of these columns we can just say this get table and we can just append that on that will
02:28
just give us the table name so we can now reuse this in any of the items that need to be archivable. Let's try this out so let's go over to our column model and we'll get rid of everything that we've added in here
02:45
and now we can go ahead and just use that archivable trait. So let's go over and try this out you can see that I can archive cards I can go over and I can put them back. Let's archive some of these cards so I'm going to archive four first then I'm going to archive card
03:05
one and hopefully they're going to be in the wrong order so card one was at the top yeah let's just try that one more time and yeah abc's at the bottom that was the last one I archived so let's add in a scope into our archivable trait
03:21
that orders these by the time that they were archived so let's say latest archived again we're going to get our query builder into here and we can just order this like we normally would so let's say query order by and we're
03:39
going to order this by archived app we don't need to use the column in here and we'll do that in descending order okay so now that we've done that over in our card archive here we can go ahead and pull this in latest archived and we can do the same thing for our
03:57
column archive as well and use that trait without having to redeclare it in both classes latest archived and we should be good so now over here we should see abc at the top because that was the last one that we archived
31 episodes2 hrs 27 mins

Overview

Get ready to master drag and drop sorting in Livewire, by building a Trello clone.

We’ll start out by building the interface completely from scratch, then add the ability to sort columns and cards, including moving cards around columns. As we sort everything, we’ll keep the database perfectly updated with the new order.

Our Trello clone will also allow us to edit column titles inline, edit cards and add notes, archive cards and columns, and put them back on the board.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!