Thursday, February 20, 2014

Jeremy's Programming Blog - Getting Started With AngularJS

JeremyMorgan.com Newsletter

 
Sent to Insider Member blogs.sherrylinx.8442@blogger.com



Jeremy's Programming Blog - Getting Started With AngularJS

Lately I’ve been playing around a little with AngularJS, and I’m pretty impressed. It’s amazing how many time saving features and structure this framework brings to your applications. I decided to build an app to calculate AdSense earnings, and I’ll demonstrate it here.

What the app does

You can check your Google AdSense earnings in realtime, and if you’re a stats nut like me, you might just do that. What I wanted to do is see if I can predict my payout of the end of the month based on that day’s earnings, and total earnings based on the current trends. So the application will:

  • Take your day’s amount and see what the total would be if you made that every day

  • Take your accumulated amount and determine what you’d make at the current rate.

The daily amount of course is just multiplied by the days of the month, but the accumulated amount is averaged over how many days are passed. You could easily do this with vanilla JavaScript but AngularJS makes it simpler and better structured. Let’s get started!

What you need

This is the easy part. You just need a copy of Angular.Min.Js and an index.html file. You should run them in a web server on your local machine, and you can modify the index with any text editor.

Let’s get started!

Initial page setup

We create a standard html webpage like so:

1  2  3  4  5  6  7  8  9  
<!DOCTYPE html>  <html lang="en">  <head>      <meta charset="utf-8">      <title>Angular Demo</title>  </head>  <body>  </body>  </html>  

We need to add a few things to make sure this is recognized as an AngularJS application:

1  
<html lang="en" ng-app>  

The ng-app directive tells AngularJS that you want to auto bootstrap that root element as an Angular application. You don’t need to make the entire HTML page an application, but in this case we will.

You will also want to include your script into the page in the footer:

1  
<script src="angular.min.js"></script>  

Now we’ll get started with the actual application.

Adding the controllers

Add the following to your body tag:

1  
<body ng-controller="TextController">  

This declares that the body element is using the controller “TextController”. Controllers are a primary means of functionality in AngularJS. This is the central part of MVC (Model View Controller) pattern that AngularJS uses.

For display purposes we want to create a placeholder in the title like so:

1  
<h2>{{someText}}</h2>  

Since AngularJS treats the HTML as a template, this is a simple placeholder you may have seen in other templating systems. Let’s change what this says. We will add a controller to the page within script tags using common JavaScript:

1  2  3  4  5  
<script>  function TextController($scope){      $scope.someText = "Angular Adsense Calculator";  }  </script>  

One thing you may notice is the parameter we’re passing to this function named “$scope”. This is our root scope which serves as a global storage area for the application. The line of code in your controller shows that we’re declaring the someText property value within the scope. Save and run this page and you’ll see that the headline changes.

Now we’ll do some real stuff.

Creating the calculator form

Here we’re going to create an AngularJS driven form that’s going to be used to handle our input values. Add the following:

1  2  3  4  5  6  7  8  9  10  11  12  
<form ng-controller="StartupController">      Today (Actual): <input ng-change="computeDaily()" ng-model="funding.dailycount"><br />     <br />     This Month: <input ng-change="computeMonthly()" ng-model="funding.monthcount"><br />      <br />      <br />      <b>Based on Today:</b><br />      Predicted Payment: {{funding.dailyaftertax | currency:"USD $ "}} <br />      <br />      <b>Based on Month:</b><br />      Predicted Payment: {{funding.monthlyaftertax | currency:"USD $ "}} <br />  </form>  

Some things to note. In the input tags we have ng-change=”“. This means when the value in this text box changes, it will call the function named inside. In this case, we’re going to call the computeDaily() function, and the computeMonthly() function.

Also, you notice the ng-model=”” parameters. These set the funding.dailycount and funding.monthcount inside our scope. Funding. is our model name.

You will notice we have two template placeholders, and they are also accessing properties in the model, funding.dailyaftertax and funding.monthlyaftertax. These two properties are also added to the funding model, which will create and use below.

Creating the calculator controller

As I said earlier, controllers are a crucial function of AngularJS and where most of the work gets done. Here we’ll create the StartupController that will do the calculations for us.

1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  
<script>  function StartupController($scope){        var date = new Date().getDate();        $scope.funding = { dailycount: 0, monthcount: 0, dailyaftertax: 0, monthlyaftertax: 0 };        var computeDaily = function() {          $scope.funding.dailyaftertax = (($scope.funding.dailycount * 28) / 1.33);      };      var computeMonthly = function() {          $scope.funding.monthlyaftertax = ((28 * $scope.funding.monthcount / date) / 1.33);      };        $scope.$watch('funding.dailycount', computeDaily);      $scope.$watch('funding.monthcount', computeMonthly);  }  </script>  

If you look at this code you’ll see a few things going on. For one, we’re getting the date of the month from the getDate() function in JavaScript. This is so we know what day of the month it is.

The next line shows how we set up our funding model. We define these four variables in the model and initialize them to zero. We will set values to these once the form is in use.

The computeDaily function assigns the dailyaftertax variable a value that takes the dailycount from the input in the form and multiplies it by 28 (the number of days in February) then divides it by 1.33 to get our rate after taxes. This calculation simply assumes how much we would make if the value you input were our daily average.

The computeMonthly function works a little different. It assigns a value to monthlyaftertax by taking how much have made so far and averaging it over the elapsed days, then multiplying that by 28, and dividing by 1.33 to remove the tax rate.

Obviously, for months other than February we’ll need to change the value 28, or determine the days present based on what month it is. For now I just hardcoded 28.

Adding in watches

You’ll notice two lines that say $scope.$watch followed by a variable and a function name. Watch is an expression that registers a listener callback to be executed when the values of those variables change. You can assign these listeners to multiple variables and even have listeners call other listeners.

The full page

By now your full page should look like this:

1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  
<!DOCTYPE html>  <html lang="en" ng-app>  <head>      <meta charset="utf-8">      <title>Angular Demo</title>  </head>  <body ng-controller="TextController">      <h2>{{someText}}</h2>        <form ng-controller="StartupController">          Today (Actual): <input ng-change="computeDaily()" ng-model="funding.dailycount"><br />          <br />          This Month: <input ng-change="computeMonthly()" ng-model="funding.monthcount"><br />          <br />          <br />          <b>Based on Today:</b><br />          Predicted Payment: {{funding.dailyaftertax | currency:"USD $ "}}<br />          <br />          <b>Based on Month:</b><br />          Predicted Payment: {{funding.monthlyaftertax | currency:"USD $ "}}<br />      </form>  </body>      <script src="angular.min.js"></script>      <script>        function StartupController($scope){            var date = new Date().getDate();            $scope.funding = { dailycount: 0, monthcount: 0, dailyaftertax: 0, monthlyaftertax: 0 };            var computeDaily = function() {             $scope.funding.dailyaftertax = (($scope.funding.dailycount * 28) / 1.33);          };          var computeMonthly = function() {              $scope.funding.monthlyaftertax = ((28 * $scope.funding.monthcount / date) / 1.33);          };            $scope.$watch('funding.dailycount', computeDaily);          $scope.$watch('funding.monthcount', computeMonthly);      }        function TextController($scope){         $scope.someText = "Angular Adsense Calculator";      }        </script>  </html>  

And when you run it, you can see the realtime AdSense calculator in action.

Summary

AngularJS is extremely powerful, testable and suited for rapid development. It’s popularity has risen dramatically and for good reason. Things are being improved and added to it daily, and people are finding best practices and establishing guidelines as we speak.

Don’t be shy! Download a copy and start digging in now!!

Video Tutorial

Here is a video I made of this tutorial:

Be sure to check it out and subscribe to my channel for more tutorials and instructional videos!

     


More Recent Articles


 

 

Thank you for subscribing to my updates! 

Follow Jeremy on Facebook | Twitter | Linkedin

 




Email subscriptions powered by FeedBlitz, LLC, 9 Thoreau Way, Sudbury, MA 01776, USA.

 

Thursday, February 13, 2014

Jeremy's Programming Blog - Just How Fast Are GitHub Pages?

JeremyMorgan.com Newsletter

 
Sent to Insider Member blogs.sherrylinx.8442@blogger.com



Jeremy's Programming Blog - Just How Fast Are GitHub Pages?

Recently GitHub rolled out some improvements to GitHub Pages, their free static page hosting service. As this is a static site I’ve recently had a lot of interest in static hosts and seeing which ones might be the best. I decided to do a comparison and see how some major services, including GitHub pages serve up static content. I was a bit surprised.

My Current Hosting

Currently I’m hosted at Arvixe Hosting and I couldn’t be happier with it. The service is great, speed is great and it’s overall a really good package. One thing I’ve noticed about them, is they serve up static pages like this one surprisingly fast:

GitHub Pages Speed Test

Couple that with the fact they run ASP 4.5 and unlimited MSSQL databases, I’m not exactly looking around for a new host. However I am curious about other static site hosting options so I did some tests.

The Initial Test

Initially I ran some tests of my page’s basic content. I generated the site in Octopress and uploaded it to the following sites:

  • Arvixe (The site you’re looking at)
  • Windows Azure
  • Amazon AWS
  • GitHub Pages

I figured they’d all be pretty close. Since host all my CSS, JavaScript and Images on a CDN anyway, they don’t factor in to the actual host speed.

These are tested with Webpagetest.org from Dulles, VA on a cable connection.

GitHub Pages Speed Test

Site Load Times (Standard Website, Dulles VA)

  • Arvixe - 2.866s
  • Windows Azure - 3.962s
  • Amazon AWS - 4.055s
  • GitHub Pages - 2.658s

So in this test we have two clear winners. Arvixe and GitHub pages. Since these tests were run from the same site I decided to change it up a bit.

Site Load Times (Standard Website, Los Angeles CA)

  • Arvixe - 3.283s
  • Windows Azure - 3.480s
  • Amazon AWS - 3.483s
  • GitHub Pages - 3.202s

The results here were a little different. The grouping is much more close, and clearly geography matters. So why not go overseas with a test? Since I have a large audience coming from India, I decided to test that:

Site Load Times (Standard Website, Indore, India)

  • Arvixe - 8.140s
  • Windows Azure - 7.347s
  • Amazon AWS - 7.872s
  • GitHub Pages - 5.915s

Here we see the sites load slower overall, but GitHub Pages are considerably faster. This is using my standard web page, so I decided to do an all text test.

Results with all text test page

I decided to create an all text page, so I set up a simple bootstrapped page with a ton of text on it.

http://www.jeremymorgan.com/speedtest/

Download this test page from GitHub

I uploaded that test to the different hosts. This one loads considerably slower but with a large chunk of text, it should give us a pretty good idea of which host serves straight text the best.

This test was so large that my India tests kept timing out. It’s a good thing my articles are never quite that verbose. Here’s the results of some tests from various geographic locations:

GitHub Pages Speed Test

Site Load Times (All Text, Dulles, VA)

  • Arvixe - 30.239s
  • Windows Azure - 22.178s
  • Amazon AWS - 32.918s
  • GitHub Pages - 14.673s

Site Load Times (All Text, Los Angeles, CA)

  • Arvixe - 32.671s
  • Windows Azure - 21.535s
  • Amazon AWS - 39.045s
  • GitHub Pages - 18.393s

Site Load Times (All Text, Miami, FL)

  • Arvixe - 41.267s
  • Windows Azure - 27.018s
  • Amazon AWS - 57.933s
  • GitHub Pages - 23.175s

Site Load Times (All Text, Denver, CO)

  • Arvixe - 79.055s
  • Windows Azure - 73.729s
  • Amazon AWS - 93.335s
  • GitHub Pages - 47.211s

Site Load Times (All Text, Phoenix, AZ)

  • Arvixe - 27.164s
  • Windows Azure - 26.16s
  • Amazon AWS - 45.765s
  • GitHub Pages - 11.263s

Site Load Times (All Text, Montreal, CAN)

  • Arvixe - 67.208s
  • Windows Azure - 45.301s
  • Amazon AWS - 60.068s
  • GitHub Pages - 44.022s

Why these tests don’t matter much

It’s easy to draw a conclusion from these results, but they’re not very scientific. The tests run are on people’s machines all around the world with various different internet connections and other variables that can go wrong. A tester’s connection could instantly slow on one site or another, or server loads can change. You never truly know your actual load time, and I cannot say “Github pages are ____% faster” with these tests.

That being said with each set of tests if you compare the sites to each other it’s pretty clear GitHub pages are faster overall. I don’t know exactly what they’re doing in the background other than what they share but it’s pretty clear they have text compression and serving down very well. If you’re building a static site it’s a pretty good bet to host it on GitHub pages if you can.

Run your own tests!

If you’d like to run a similar set of tests, head over to the GitHub page for my static test page and upload it anywhere you want. I used webpagetest.org for the tests, and used Imacros to automate the process. Let me know in the comments or contact me if you come up with something different!


Do you like articles like this?

I’m constantly hacking on stuff and writing about happenings in the programmer world. You can subscribe to my feed here, or you can get the programmer newsletter 100% spam free!
     


More Recent Articles


 

 

Thank you for subscribing to my updates! 

Follow Jeremy on Facebook | Twitter | Linkedin

 




Email subscriptions powered by FeedBlitz, LLC, 9 Thoreau Way, Sudbury, MA 01776, USA.