Now let's allow our users to update and delete songs. We're going to create a new modal for the updating part.
Here's a rough idea of what the view could look like:
Make sure your modal's elements don't use the same ids as other modals on the page!
Objectives:
- allow users to edit songs
- allow users to delete songs
- practice more with Bootstrap modals
Howdy! Good work, partner! If you're at sprint 6, you must be rarin' to go! We're gonna ask you to solve some problems on your own in this sprint.
Now might be a good time to take another look at the bootstrap modal documentation.
We'll create another modal in index.html
. Each time someone clicks a button to edit songs, we'll give the modal the current songs' information. This won't be too hard since we're just going to replace the modal body (on the sample below, look for #editSongsModalBody
).
-
Add a new modal to the page. You can build your own OR use the sample provided.
If you're using the sample, take a look at the unique ids created on the elements. We'll be using those later on.
-
Add a new button 'Edit Songs' in the
panel-footer
of each album row. -
When 'Edit Songs' is clicked, open the modal!
hint: opening a modal with Bootstrap's JavaScript
$('#fooModal').modal('show');
-
Develop a form for editing the song list. It should be able to (1) delete a song (2) edit each song (see a possible layout image above). Your form should be an HTML template string in your javascript.
-
Remember that in order to do a
DELETE /api/albums/:album_id/songs/:id
or aPUT /api/albums/:album_id/songs/:id
you'll need thoseid
s. Track them asdata-
attributes in your form. -
You may want to use a
GET /api/albums/:album_id/songs
index route to get all songs for a particular album. This is likely easier than retrieving incomplete data from the page.Sample HTML for the form is provided for you. You can use this to help you develop your template.
If using the sample form or following the wireframe, note that you can make each row a separate form if you want.
-
Create the server-side route for
DELETE /api/albums/:album_id/songs/:id
. -
Write client-side JavaScript to delete a song from the form when the corresponding delete button is clicked. (Make sure the deleted text
<input>
is removed as well as the button. -
Test delete for songs.
You may want to read about subdocs. In particular, check out the section on finding subdocs and removing subdocs.
-
Ensure that the song list on the page (the main album row that contains this song) is updated as well.
You may want to re-retrieve the songs rather than trying to parse the current album
<li>
content. It would be a good idea to make a function for this. It'll be useful in the next step.
Let's allow users to save their edits.
-
Create the server route and skeletal controller method for
PUT /api/albums/:album_id/songs/:id
. -
After the user clicks a 'Save' button, make an AJAX request to edit the song.
-
Finish the code to handle the request on the server-side, if you haven't yet.
-
Update the page with the changed song.
-
Make sure you test everything.
-
Make sure the song list form modal closes when the close button is clicked.
Ideally you would send each update, and after the responses for all the updates have been received; then you'd request the album and update the page. This is a super-super-stretch exercise and a really tough challenge that would probably use a technology called promises. Feel free to take the easier approach of just requesting the album and updating the page after every response to your PUT request.
- Add functionality so that the user can create new songs and have them added to the list from within the modal.
-
Add a saving spinner or animation for each song when it is saving.
-
Save each song when the user leaves its input box.
-
Client-side validations: make sure
trackNumbers
are numbers & unique. In displays, sort songs bytrackNumber
. -
Consider using a Bootstrap theme.
-
Consider using Font Awesome.
-
Server-side validations: make sure track numbers are unique within each album.
If you got this far, we are SUPER impressed. Congratulations!