How To Add Records With Mongo And Ember

This is an ongoing series of posts where I cover how to create a Node/Express/Mongo back-end with an Ember front end. We've already created a REST serverinterface in a previous post then we added in Mongo and finally we added in an edit and delete quote feature.

With all that said we still can't add a new quote yet. We'll be looking into that today. As always I've been updating the Node server code and the Ember application code on Github. You can use this to help follow along.

Node Server Setup

The good thing is that we already have the http POST module already created in our Express server (check back the previous blog posts if you want to learn more about this). If you remember we used CURL and Postman to add new quotes to our database. The api/quote.js file should have the addQuote module in it.

The addQuote will get triggered when visiting the /quotes post route. This should be it on the Node server side. Everything is already in place to move onto the front-end.

Ember

Since we already have everything in the Node server in place all we need to do is add a way to add quotes. By default Ember data is using the REST adapter so it knows to send a POST message to the server when you save the model. Let's begin by creating a new template and controller.

Here we are creating a new quote record. Then we clear the values and save it. Last we transition back to the route. As I mentioned before this will send a http POST message to the server in JSON. Since we already set everything up in our Node server this should work.