Skip to main content

Ionic app with a Django backend: Part 2 - The Ionic app

In this post, I will continue from where we left in Part 1 of this series.  In part 2, I will talk more about the front-end side of things. Ahh, I have a background in web development, hence I call this front-end, but that's not the most accurate thing to say in these modern times. So the right way to say this is, I will talk about the Ionic app in this Github repository(IonicAppWithRestBackend) in more details in this post.

So what did we do in part 1?

In part 1, we got to the point of running our backend on the web server that ships with Django. To refresh your memory, this is how we got the backend server running,

Clone the repo
git clone https://github.com/cptdanko/IonicAppWithRestBackend
Navigate to the backend project directory
cd IonicAppWithRestBackend/backend/python_django/restBackend/
Note: if you are using Windows, you may want to change "/" to "\" in the above command.

Once there you can run the backend server with the following command
python manage.py runserver
At this point we should have out backend running on http://localhost:8000/, which exposes two RESTful endpoints that can be accessed like this
http://localhost:8000/smugglers/
and 
http://localhost:8000/smuggler/id/

I will talk more about what these end points do a bit later, but for now lets talk about the Ionic app.

So how do we run the Ionic app?

To run the Ionic app in this project, in the folder where you cloned the repo i.e. IonicAppWithRestBackend, navigate to the following directory 

cd IonicAppWithRestBackend/ionicApp/
Note: Windows users watch out for the "/"

and run the following command

ionic serve
This will start the Ionic app and you can test it in the browser using at http://localhost:8100. 

What is the Ionic app in this repo?

The Ionic app is a simple sidemenu app which fetches some data from the backend and displays it . The screenshot on the right shows what the app looks like.

When you run the app, it fetches some data(names of 2 smugglers) from the 

http://localhost:8000/smugglers/

endpoint from our backend. Data is fetched using the $http service in AngularJS. In addition to the official AngularJS docs, you can also look at the tutorial: Integrating a Backend Service with AngualrJS on Learn Ionic

If you click on Hondo or Han the app will use the id of the smuggler to fetch more details about him from this endpoint

http://localhost:8000/smuggler/id/

and show that info using an Ionic popup.

p.s. regarding the data, hmmm...let's just say, I am looking forward to Dec 2015!

So any other problem solved to be able to have a solution like this?

Since both our Ionic app and the RESTful backend are running on localserver, I ran into the CORS issue when I first started working on this. Thankfully this blog post on the Ionic blog provides a solution to this owing to which the ionic.project file for the Ionic app in this repo(IonicAppWithRestBackend) looks like this,
"proxies": [
    {
        "path":"/api/",
        "proxyUrl": "http://localhost:8000/"
    }
]
Now, if you look at the code in HomeCtrl.js in this repo, the urls we pass to the $http service look something like
/api/smugglers
or 
'/api/smuggler/'+smugler.id+'/';

That combined with the proxy that we have in our ionic.project file means that every time our app is calling a url which has api in it's path, the api part of the url will be replaced with
http://localhost:8000/ 

so when we call(invoke)

/api/smugglers/ 

thanks to our proxy, the actual url invoked will look like this

http://localhost:8000/smugglers/

Any last words?

None actually! Like I said in part 1, should you have any questions about this or suggestions on how I can improve this repo, then please leave a comment.

The fate of the project with my associate

To get an idea of what project am I talking about, have a read of the motivation section in part 1 of this post. To avoid any distractions from the actual content of this post, I was saving this for the end. So the project for which I had to build a solution like this: after a year of working on it, it came to a very sudden and somewhat quick finish. The best way to summarise it would be.. it was like a girl breaking up with a guy over a text message. So this is the summary of our breakup conversation, 

Associate: 'Bhuman there have been problems from the beginning and I am done now'
Bhuman: 'But we can make this work, it's been a year and you never mentioned this'
Associate: 'No, I can't do this anymore. It's not me, it's you and I don't think you can change!'


Yeap that is pretty much how it happened. So there was no talking about it or any warnings prior to this, the only time she mentioned anything was when she was ending it. Ohh and all of this was over email. There's more to the story, she used a few(harsh) words that she really shouldn't have but I will spare you the details. You know summarising all of it this way and given how it happened it's hard not to see the funny side of it.


Lastly, I am working on my startup full-time right now, so if you find my blogposts useful and want to see me share more useful stuff in the future, please do support me. You can support me, 

Whichever version you try please leave us an app store review from you would be helpful. 

Comments

Popular posts from this blog

Upload to AWS S3 from Java API

In this post, you will see code samples for how to upload a file to AWS S3 bucket from a Java Spring Boot app. The code you will see here is from one of my open-source repositories on Github, called document-sharing. Problem Let’s say you are building a document sharing app where you allow your users to upload the file to a public cloud solution. Now, let’s say you are building the API for your app with Spring Boot and you are using AWS S3 as your public cloud solution. How would you do that? This blog post contains the code that can help you achieve that. Read more below,  Upload to AWS S3 bucket from Java Spring Boot app - My Day To-Do (mydaytodo.com)

Addressing app review rejections for auto-renewing subscription in-app purchase (iOS)

The ability to know what the weather is like while planning your day is a feature of  My Day To-Do  Pro and as of the last update it’s also a part of the  Lite version . Unlike the Pro version it’s an auto-renewing subscription based  in-app purchase (IAP)  in the Lite version. What means is that when a user purchases it, the user only pays for the subscription duration after which the user will be automatically charged for the next period. Adding an  auto-renewing  subscription based IAP proved to be somewhat challenging in terms of the app store review i.e. the app update was rejected by the App Review team thrice because of missing information about the IAP. Therefore in this post I will share my experiences and knowledge of adding auto-renewing IAP in hopes to save someone else the time that I had to spend on this problem. In-App purchase This year I started adding IAPs to My Day To-Do Lite which lead to learning about different types of IAP...

Serving HTML content in an iOS app that works in iOS 7 and later (using Swift)

As I have mentioned in an earlier post , I really enjoying coding in Swift. Now what am I doing with it? Well I am trying to build an HTML5 app that must work on devices with iOS 7. So in iOS8 apple has introduced a whole bunch of features that facilitate easy communication between web content and lets just call it back-end Swift code, but those features are not in iOS 7. So why do I want to build something that would work in an older OS? well I do not expect existing iOS users to upgrade to iOS 8 straight away and i also know a couple of people who would be very reluctant to upgrade their iPhones to iOS 8. Now in case you do not, you can have a read of the "Working with WebViews" section of this post , to know how to serve HTML content with WebViews. So when I started building my app, I wanted to know: How do I invoke some Swift code from my HTML content? Well the solution to this may feel a little bit "hacky" but it is a solution to achieve this.  The followi...