Saturday, April 25, 2015

Week 5 - Customized Services and Routes

This week we are going to revisit services (Code School just touched upon them) and introduce routes (used as one expands the app to use multiple screens).

Assignment
Fork the fiddle "AngularJS: A Walk-Through - Week 4" and
  • Name it "AngularJS: A Walk-Through - Week 5"
  • "Update" it and "Set as base". 

Services

In the example we have been using, the gems data was simply jammed into our controller; not pretty and not reusable. We are going data off into a separate object called a service.

Assignment
Read articles describing AngularJS services; in particular:

AngularJS Developers Guide: Services
<https://docs.angularjs.org/guide/services>

If one read other articles on services, they would have likely bumped in a number of other types of services other than a factory, e.g., service and provider.  Yes, it is unfortunate they they used the word service to mean two different things.  The good news is that for the most part these others are either redundant or used in edge cases.  We will not use them.

First, we are going to stub out a gems service (to be added to ends of the JSFIDDLE JavaScript pane).

JavaScript
// gems.js
(function() {
var app = angular.module('store-services', []);
    app.factory('gems', function() {
        var service = {};
        return service;
    });
})();

Next we are going to have the "gemStore" app use the "store-services" by updating the following at the top of the JSFIDDLE JavaScript.

JavaScript
    var app = angular.module('gemStore', ['store-directives', 'store-services']);

Next we are going to move the "gems" definition  out of the "StoreController" and into the "gems" service by simply replacing the empty object "{}" with the array of gems.

Finally, we are going to inject the "gems" service into the "StoreController" as follows:

JavaScript
    app.controller('StoreController', ['gems', function(gems){
        this.products = gems;
    }]);

Assignment
Replicate the work above in the fiddle "AngularJS: A Walk-Through - Week 5" ("Update and "Set as base")

As one can see, this did not add any functionality; rather this set us up to use the "gems" service in other parts of this application.

Assignment
While this was covered a bit in the Code School material and we just used it above, read articles on AngularJS dependency injection; in particular:

AngularJS Developers Guide: Dependency Injection
<https://docs.angularjs.org/guide/di>

Routing

Angular uses different hash URLs, e.g., <http://domain.com/#/> and <http://domain.com/#/login> to differentiate screens as one navigates the app.  Like any URL they can be bookmarked and crawled independently by the search engines. The important thing to understand is that the browser is not reloading the page as it navigates to a different hash URL.

Angular uses a separate ngRoute module to handle navigation between screens within an app.

Assignment
Read articles on AngularJS routing; in particular:

AngularJS: Tutorial: 7 - Routing & Multiple Views
<https://docs.angularjs.org/tutorial/step_07>

We are going to simply use the ngRoute module with the existing functionality (still only one screen). We first install the ngRoute module from a CDN using JSFIDDLE "External Resources".

//ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular-route.js

Then we make the "gemStore" application depend on ngRoute.

JavaScript
 
var app = angular.module('gemStore', ['ngRoute', 'store-directives', 'store-services']);

We then add an app.config to the gemStore application:

JavaScript
 
    app.config(['$routeProvider', function($routeProvider) {
        $routeProvider.
        when('/', {
            templateUrl: 'store.html',
            controller: 'StoreController'
        }).
        otherwise({
            redirectTo: '/'
        });
    }]);        


Then we move the HTML (the top part that is not part of a ng-template script section) into the "store.html" file using the ng-template syntax.

HTML
 
<script id="store.html" type="text/ng-template">

Finally, we insert the ngView directive to the top of the HTML.

HTML
 
<div ng-view></div>

Assignment
Replicate the work above in the fiddle "AngularJS: A Walk-Through - Week 5" ("Update and "Set as base")

As one can see, this did not add any functionality; rather this set us up to add additional screens.

Assignment
This challenging assignment is to use what one knows to add an "about.html" page that is linked from the "store.html" page.  Things to remember:
  • Add the appropriate routing entries to the "app.config" section, e.g. "about.html" and "AboutController".
  • Create a do nothing "AboutController".
  • Create the "about.html" page using the ng-template syntax.
  • Use a hash URL to link between pages.

Sample of Week Result
The result of this week is an AngularJS app, "AngularJS: A Walk-Through - Week 5" that will be used as a starting point for the next week.

AngularJS: A Walk-Through - Week 5
<http://jsfiddle.net/sckmkny/y04ht4mt/>

Saturday, April 18, 2015

Week 4 - Shaping Up with Angular.JS by Code School

This week we are going to wrap up the "Shaping Up with Angular.JS" by Code School course.

Much of the work this week involves refactoring the existing code into separate directives and then grouping them into modules.

Assignment
Fork the fiddle "AngularJS: A Walk-Through - Week 3" and
  • Name it "AngularJS: A Walk-Through - Week 4"
  • "Update" it and "Set as base". 

Assignment
Complete the levels
  • Level 4: Custom Directives
  • Level 5: Services

    Assignment
    As one completes the challenge "5.2 Refactoring into a Module", replicate the solution in the fiddle "AngularJS: A Walk-Through - Week 4" ("Update and "Set as base") . Some things to keep in mind:

    note: Notice that we are replicating 5.2 and not the last challenge.  This is because we will do our own example of a service in a later week.

    • Remember the ng-app and the "StoreController" ng-controller directives are in the body tag accessible from the "Fiddle Options"; so one only needs to copy the content of the body tag to the JSFIDDLE HTML panel.
    • Because JSFIDDLE does not support multiple files, we are going to need to build both the app.js and the products.js in the JAVASCRIPT panel; just copy and past them one after the other.
    • Similarly, we will need to build all the separate HTML files into the single HTML panel. Luckily AngularJS provide a mechanism to simulate separate HTML template files by wrapping the HTML with filename "FILENAME" with the "<script type="text/ng-template" id="FILENAME">" and "</script>" tags.
    • Repeat the inclusion each of the templates below the index.html content, e.g.,
      • product-description.html
      • product-specs.html
      • product-reviews.html
      • product-tabs.html
      • product-gallery.html
    • Remember to fix the images in the gems object with the prefix:
      http://dhg7upb7j7jqa.cloudfront.net/shaping_up_with_angular_js/assets/demo/

    One oddity in the solution to the 5.2 challenge above is that they encapsulated the controller in the productsTabs directive but not the productReviews directive.  We will fix that.

    Assignment
    Move the functionality of the "ReviewController" into the "productReviews" directive.
    1. Create both "controller" and "controllerAs" properties of the "productsReviews" directive.
    2. Set the value of the controller to be the anonymous function of the "ReviewController".
    3. Set the value of the "controllerAs" to be "reviewCtrl" (in this case, include the double quotes")
    4. Delete the ReviewController definition.
    5. Delete the "ng-controller" directive from the form with name "reviewForm".

    Sample of Week Result
    The result of this week is an AngularJS app, "AngularJS: A Walk-Through - Week 4" that will be used as a starting point for the next week.

    AngularJS: A Walk-Through - Week 4
    <http://jsfiddle.net/sckmkny/033hgu38/>

    Sunday, April 12, 2015

    Week 3 - Shaping Up with Angular.JS by Code School

    This week we are going to do only one Code School level (Level 3: Forms) to allow some folks to catch up from last week.

    One interesting observation to make as one looks over the resultant application from this week's work is just how little JavaScript (approx. 30 lines outside of the embedded data) is needed for the functionality provided.

    Assignment
    Fork the fiddle "AngularJS: A Walk-Through - Week 2" and
    • Name it "AngularJS: A Walk-Through - Week 3"
    • "Update" it and "Set as base". 

    Assignment
    Complete the level
    • Level 3: Forms

    Assignment
    As one completes the final challenge "3.10 Form Styling", replicate the solution in the fiddle "AngularJS: A Walk-Through - Week 3" ("Update and "Set as base") . Some things to keep in mind:

    note: Notice that we are replicating 3.10 and not 3.11 (the last challenge).  This is because the Code School folks neglected to build 3.11 on top of 3.10.
    • Remember the ng-app and the "StoreController" ng-controller directives are in the body tag accessible from the "Fiddle Options"; so one only needs to copy the content of the body tag to the JSFIDDLE HTML panel.
    • Remember we are going to simply insert the app.js JavaScript in JSFIDDLE by copying it into the "JavaScript" panel.
    • Remember to fix the images in the gems object with the prefix:
      http://dhg7upb7j7jqa.cloudfront.net/shaping_up_with_angular_js/assets/demo/

    Assignment
    We are going to update our JSFIDDLE to implement the created on date as done in "3.11 Showing CreatedOn Date".

    note: Remember; one cannot simply copy the entire HTML and JavaScript as Code School neglected to build 3.11 on top of 3.10.

    Apply the three objectives of "3.11 Showing CreatedOn Date" to the JSFIDDLE example.

    Sample of Week Result
    The result of this week is an AngularJS app, "AngularJS: A Walk-Through - Week 3" that will be used as a starting point for the next week.

    AngularJS: A Walk-Through - Week 3
    <http://jsfiddle.net/sckmkny/nhsk8npu/>

    Sunday, April 5, 2015

    Week 2 - Shaping Up with Angular.JS by Code School

    This week we are going to learn the basics of AngularJS.

    Assignment
    Fork the fiddle "AngularJS: A Walk-Through - Week 1" and
    • Name it "AngularJS: A Walk-Through - Week 2".
    • In "External Resources" add Bootstrap 3.1.1 from the following CDN
    <//dhg7upb7j7jqa.cloudfront.net/shaping_up_with_angular_js/assets/demo/bootstrap.min.css>

    note: Using a newer version of Bootstrap (from a standard CDN) will introduce subtle differences from the Code School example. 
    • "Update" it and "Set as base". 

    Assignment
    Create a free account at:

    Code School 
    <http://www.codeschool.com>

    Start the course "Shaping up with Angular.js" listed under the JavaScript language path.

    Assignment
    Complete the levels
    • Level 1: Flatlander's Gem Store
    • Level 2: Built-in Directives

    Assignment
    As one completes the final challenge "2.10 Using Gallery Controller", replicate the solution in the fiddle "AngularJS: A Walk-Through - Week 2" ("Update and "Set as base") . Some things to keep in mind:
    • As JSFIDDLE does not have a mechanism to modify the HTML tag, use the "Fiddle Options":  "Body tag" setting instead, e.g.,
      <body class="list-group"  ng-app="gemStore" ng-controller="StoreController as store">
    • Instead of linking to a external JavaScript file "app.js" in the <head> as in the  Code School example (not a feature of JSFIDDLE), we are going to simply insert the JavaScript in JSFIDDLE by typing in the "JavaScript" panel.
    • To show the images, add the prefix:
      http://dhg7upb7j7jqa.cloudfront.net/shaping_up_with_angular_js/assets/demo/
      before each of the image strings.

    Sample of Week Result
    The result of this week is an AngularJS app, "AngularJS: A Walk-Through - Week 2" that will be used as a starting point for the next week.

    AngularJS: A Walk-Through - Week 2
    <http://jsfiddle.net/sckmkny/n6p5v0b9/>