It has been a while since my last update to the web app, but a lot has been going on. With some extra time here before Thanksgiving, figured it would be worth a getting back up to speed and pushing onwards!

Where we left off

We left off with the Sign Up controller, and this is a main reason it has taken me so long to get back to it. Currently the WP-API does not support a way to create new users from the API endpoints. For other applications I have used with WP-API I had to create a specific endpoint just for that. I am working on submitting my updated core to WP-API to be a part of the newest version and the version that makes it into WordPress core, however at this time, it is not.  What does that mean for our app? Well, we can just skip the User registration and get right into the fun part!

I think we should start with creating a new post, specifically a custom post type. The workflow of creation or getting to the template part is up to your discretion, I prefer to go

  1. Route
  2. Factory
  3. Template
  4. Controller

Sometimes I switch the last 2, depending if you need to get data before for the view. For example on a page where you view all the posts, you might want to create the controller first so you can define $scope.posts

Creating a route

First we are going to create a route for our application to point to. Remember this code goes into ‘assets/js/wp-app/wp-app-routes.js’ file.

Just like the signup route, we are going to keep it simple. Define the path ‘/new-style-guide’, the controller and the template URL.

Creating a factory

Creating a factory is next, this way we don’t have to worry about any issues when we are creating our controller.

The api endpoint is the same one for all posts (or pages) ‘/posts’. We add in a parameter for the Custom Post Type (type[]=style-guides) so that we can do CRUD actions against that endpoint.

Creating the controller

I was originally going to make a controller that would just be for creating new Style Guides, however because there isn’t much to creating a new post I want to lump all Style Guide functionality and interaction with the factory into 1 single controller. Luckily AngularJS allows multiple routes to load the same controller, and we can always load in new controllers later.

There isn’t much going on here, I’m just doing a query or a GET request on the endpoint and defining ‘$scope.sg’ as the response, which is a JSON object of all the posts.

Creating the template

Lastly here I am going to work on a basic template that has a form that we can fill out and create a new post with. We will come back to the controller to deal with the data and saving, but for now lets just put all the HTML in place.

I am going to create a new post for the logic and creating the post because I want to show you step by step what I am doing, completing it with the final code.

GitHub Repo

Don’t forget the GiHub repos where you can collaborate on the project is ready.

There are 2 comments

Comments are closed.