In this lesson, we are going to start to build out the edit and create bookmark functionality in Eggly. In doing so, we are going to dig a little deeper into ui-router, as well as perform some housecleaning along the way.
Speaking of housecleaning, the first thing that we're going to do is delete the main controller. It has been good to us, but it's time to move along. We'll start to move out the pieces of specific functionality into more appropriate places within the Eggly application.
Within the index.html file, we will delete the ng-controller reference. In eggly-app.js, we will go ahead and just delete this controller entirely.
Now that we have cleared the deck, so to speak, let's take a moment to review the routes or states that we have in place, because each one builds on top of each other.
This will be helpful as we get into building our edit and create states. At the top level, we have this abstract Eggly state that serves as a placeholder or a namespace for application states.
We can see this state being built upon in the categories.js file, where we define the eggly.categories state. This state has two named views, a categories and bookmarks view.
The categories view targets the UI element named "categories" in the top-level state, which is Eggly. The bookmarks named view targets the UI view named "bookmarks," also, in the top-level state.
In the bookmarks file, we have defined an eggly.categories.bookmarks state that also targets the bookmarks named view in the top-level state.
Before we start to build out the create and edit states, let's go ahead and just build out the templates by refactoring the bookmarks template. I'm going to hop in, take this edit form, and just paste this into the bookmarks edit template. We can delete this.
I'm going to take this create form. We will just paste this into the bookmarks create template. I'm going to delete this ng-show, because we want this form to show at all times when we're in a create state.
I'm going to go ahead and close that. From here, I'm just going to convert this div wrapper into a ui-view element. We're going to leave this as an unnamed ui-view element that we'll revisit in just a moment.
Now that we have the template refactored into the smaller, more specific templates, let's go into the bookmarks create module and define the create state. We are going to define a config block, inject stateProvider in.
We will define our first state is eggly.categories.bookmarks.create. We'll give this a URL of bookmarks/create. The template URL is going to be...
The controller will be CreateBookmarkCtrl as create controller. Let's go ahead and stub out this controller so that it's there when we get to future videos.
Let's go ahead and do the same thing for the edit bookmark state. We'll hop into the edit bookmark sub-module. We will define our config block, inject stateProvider in.
The state will be eggly.categories.bookmarks.edit. The URL will be bookmarks. We do need to pass in a bookmarkID since this is dependent on the bookmark that we're editing.
We'll do edit, and then, the templateUrl will be app. We will define the controller as "EditBookmarkCtrl as editBookmarkCtrl." We will just go ahead and build out this controller just real quick.
We have successfully defined the two states for creating and editing a bookmark. We need to do a few additional steps so that we can successfully navigate to these newly created states.
We are going to hop into the CategoriesModel, create a few new methods, and add an additional property to keep track of the current category we are on.
Let's define a new property called "currentCategory." Then, we'll go down here. We will create a method called "setCurrentCategory," that accepts a categoryName.
From here, we will return the result of getCategoryByName, pass in the categoryName. We'll take this category that is returned from this. We'll set it to currentCategory.
Then, let's define two more methods, which are fairly simple in nature, getCurrentCategory, which just returns currentCategory, and getCurrentCategoryName, which checks to see if currentCategory exists. If it does, it returns currentCategory.name. If not, it just returns an empty string.
Now that we have modified the CategoriesModel, now that we have defined the CategoriesModel, let's inject it into the bookmarksListCtrl. Instead of setting this locally, we will pass this into or we will delegate this to the CategoriesModel and pass in the stateParams.category property.
From here, while I'm looking at this, let's just update result to bookmarks so that it's a little bit more self-descriptive. Then, we're going to pull some methods off of the CategoriesModel and make them available to the local BookmarksListCtrl. One of them is getCurrentCategory. We'll do the same for getCurrentCategoryName.
With that in place, we are ready to actually wire up this template. The first thing we need to do is update this ng-if to target the bookmarksListCtrl.getCurrentCategory. If a current category exists, then, we will show this, as well as this ng-click here to initiate the creation of a bookmark is no longer valid.
We're going to replace this with a ui-sref. We're going to navigate to eggly.categories.bookmarks.create. Let's also update the ng-click event for going into the edit state. We will replace this with a ui-sref. We're going to navigate to eggly.categories.bookmarks.edit. We're going to pass in a bookmarkID.
Let's go ahead and update this filter here to call getCurrentCategoryName. Let's just do one other thing real quick, since I'm here. Let's go into the categories template.
I want to actually allow a click on the logo to reset or go back to the default category state. We'll delete that ng-click. ui-sref. We'll navigate to eggly.categories.
I believe I've covered everything. Let's hop into the browser, give this a spin, and see if this is working. We'll select Development, Design. You can see now we're filtering on the current category.
I can click the logo, and it goes back to the base category state. From here, if I click "Edit," then, you can see it's navigating to the edit state. I can go back. If I click "Create," then, you can see it's navigating to the create form.
We have covered quite a bit of material in this lesson, but the main thing that I'd like to just quickly go over, that we did, is we created some methods on the CategoriesModel to expose the current category that we are on.
Then we created two new routes, one to edit a current bookmark and one to create a new bookmark. Stay tuned for the next lessons, where we will continue to finalize the functionality in the Eggly application. See you next time.