Saturday 16 November 2013

Make your own email app using AngularJS - Part 2

In my previous post, I had started with the basics needed to get an angular application running. Before we add more features let us begin re factoring what we already have.

I've mentioned that our controllers are in the global scope. Let’s fix that.
I am going to assign a value to the ng-app directive. So now when you view the DOM through Firebug, you will notice one global variable which is the app name we assigned to ng-app.

<html ng-app=”emailApp”>

We’re not done here. We need to register emailApp as an angular module for it to work.

Create a new JS file called app.js, with this.

var emailApp = angular.module(‘emailApp’, []);

//we have no dependencies as of now, hence the empty array.

We now need to map our controller(s) to this module. Let’s take this opportunity to migrate the controller code from the html to its own file.


Create a new JS file called controllers.js, with this.

emailApp.controller('EmailCtrl', function($scope) {
                $scope.messages = [{
                                id: 0,
                                sender: 'jean@somecompany.com',
                                subject: 'Hi there, old friend',
                                date: 'Dec 7, 2013 12:32:00'
                }, {
                                id: 1,
                                sender: 'maria@somecompany.com',
                                subject: 'Where did you leave my laptop?',
                                date: 'Dec 7, 2013 8:15:12'
                }, {
                                id: 2,
                                sender: 'bill@somecompany.com',
                                subject: 'Lost python',
                                date: 'Dec 6, 2013 20:35:02'

                }];
});
Now make sure to add app.js and controllers.js to the html page. The screen should come up as before.
There’s no fun in displaying hardcoded data. Let’s ajax-ify it.

To make use of ajax, we need to make use of an ajax service. In angular, services are of 2 types built in and user defined. I will now make use of the built in $http service to make ajax calls. User defined services will be added soon.
Before getting started make sure to move the messages array to a json file. I will save this json as messages.json

The controller now needs to have an extra parameter passed in. That parameter is $http. $http will be injected into our controller by angular. Our controller now looks like this,

emailApp.controller('EmailCtrl', function($scope, $http) {

	$http({method: 'GET', url: 'messages.json'}).

		success(function(data, status, headers, config) {

    		     $scope.messages = data;

  		}).

  		error(function(data, status, headers, config) {

		    console.log("An error occurred. Please refresh.")

		});

});

For these changes to work, we need to run this app using a web server.



Points to note
  • It is recommended to maintain each controller in its own file, rather than using a single controllers.js
  • Most built in services start with the $. It is recommended not to make use of $ for user defined services.
SPA features coming up in part 3.


Saturday 9 November 2013

Make your own email app using AngularJS - Part 1

     Angular JS is the one of the frameworks I wanted to get started from a long time. Angular, being a product by Google is the reason that got me interested.
So I am going to keep this very simple and write a mail client application. I am not going to lie, I’ve been reading a book on angular. This post is going to be part 1 of “How to build a Gmail killer”. Ha ha. If you find any mistakes, let me know.

To start off, I am going to get started with a simple html page, index.html

What you see below is a standard html5 page. I am dropping angular.min.js and bootstrap.css into the body and head sections respectively. Don’t bother about the file hierarchy for now.  The folks over at Google recommend using Yeoman. Its a long process, so I am going to skip this for now. But worth the effort.

Now the ng-app directive signals angular to kick in at this point. I have placed ng-app on the root node, that is <html>.


Everything else you see on the page is not related to AngularJS. I’ve just added a fancy navbar to our application using Twitter bootstrap.





The screen below shows the actual code for our application. "ng-controller" is assigned a controller name which will manage the div.  Note the usage of “ng-repeat” directive by the <tr> tag.



It’s now time to define EmailCtrl, which is our controller. For now, this controller maintains a hardcoded copy of our email messages.


And that’s it. Open this up and we have our very own email client.



Key points to note:

  • The function “EmailCtrl” is currently defined in the global scope. We’ll rectify that once we start with modules. Also it is recommended to move this script into its own file, say controllers.js.
  • $scope is a very important variable. Look up the Angular docs for a thorough reading. It is injected by the angular framework. Dependency injection brought to you by Angular.
  • The {{ }} are angular’s way to indicate the variable which is bound to a particular tag. So <td>{{ mail.sender }}<td> means that the value of “mail.sender” will be substituted in its place. An alternative to this is this,<td ng-bind=”mail.sender”></td>.
In subsequent posts, I will build upon this example to add SPA features, where we will show a message viewer to view individual emails.
What’s an email client without asynchronous loading? I will bring in ajax support using $http.

Stay tuned.

Thursday 4 April 2013

Of Typography and web fonts


While coming up with one web page, I thought of using some good fonts to grab attention. So I started weighing my options.
1) You could either choose to host your own system fonts onto your web server. And use this to get it onto your web page

@font-face {
      font-family: "MyAwesomeFont";
      src: url("http://mywebsite.com/MyAwesomeFont.ttf");
}
body {
     font-family: " MyAwesomeFont ", serif
}

But this sounds very old and not trendy enough. We are in the age of clouds and CDN’s. Hosting static content on your server is passé.

2)      The next option is to make use of an online font library. Two big names here, “Typekit” by Adobe and “Google Web Fonts” by elgooG. Typekit being a paid service, I started diving into GWF.
Head over to http://www.google.com/fonts. Choose a couple of fonts you like, review your collection and start using them right away.

There are 3 ways to implement the chosen fonts onto your page.
1)      The standard approach by using <link href=””>
2)      Using @import url()
3)      Using Google WebFont Loader

You can find the actual code snippets for the above 3 methods on google.com/fonts.
After that you can have this(below) to get it working,
body {
     font-family: "MyAwesomeFont1", “MyAwesomeFont2”, Arail, serif
}

Always have a couple of fallback fonts on your font-family declaration. The order in which the fonts are declared here decides which font to use when the preceding font is not found. In this case, it will fall back to a system default serif font if none of the other 3 fonts are found.

The terms serif and sans-serif are not actual fonts as such, but refer to generic font families. This image should help you in case you’ve never heard of serif or sans-serif.



The great Steve Jobs learned typography. You can see that reflecting in the designs that apple has come up with. The i(Mac/ Pod/ Phone) are all built with quality in mind. The same reflects in their UI. Each app that gets published on iTunes undergoes a lot of quality control checks. For more info on these https://developer.apple.com/appstore/guidelines.html

Ok, so I was happy after all these awesome fonts on my page. But then connectivity started breaking down. The fonts were not getting loaded at just the right moment. The result, the browser shows the text initially in a default font and once the font is actually available (downloaded and ready) it swaps the font. What I just witnessed was a FOUT(Flash of Unstyled Text).