Workshop
AngularJS 1.x Team Lead
clone using git
git clone https://github.com/petebacondarwin/foodme-further
or download zip file
https://github.com/petebacondarwin/foodme-further/archive/master.zip
or copy from USB stick
Make sure you have all these installed
Serve the application from a local webserver
A JavaScript library for writing unit tests
angular-mocks.js provides Jasmine helpers
http://karma-runner.github.io/0.12/index.html
Install & configure Karma test runner
Create and run initial unit tests for `AppController`
Write and run unit tests for AppController
Angular provides various mock services to help with testing
$httpBackend.when(...).respond(...);
$httpBackend.flush();
$rootScope.$digest();
Create more unit tests for `AppController` methods
When injecting we can wrap params in underscores
inject(function(_$http_) { ... })
There are two ways to get hold of a filter
Create unit tests for `rating` filter
The general approach is:
We can clean up our tests by defining custom matchers:
expect(element.find('li')).toHaveClass('selected', 3);
Create unit tests for `fmRating` directive
Implement routing to enable multiple URL driven views
Move the current HTML into a new `restaurants` view
Add additional static views and navigation links
Install & configure protractor
Create and run basic e2e tests
Use a PageObject to make e2e tests clearer
Move `restaurants` route and logic into its own module
Update the views and tests
$location.path() provides the relative path to the current route
Add CSS classes to show currently selected route
Provides hooks to trigger animations declaratively
Applying a CSS class to an element, in combination with ngAnimate specific CSS classes, allows you to trigger animations with an animation enabled directive
Use ngAnimate to animate navigation
Routes can have resolves
These are promises that must be resolved before the route change can take place
The resolved values are available to the route's controller
$routeProvider
.when('/restaurants', {
templateUrl: 'components/restaurants',
controller: 'RestaurantsController as component',
resolve: {
restaurants: 'restaurantListPromise'
}
});
Refactor restaurant data management into a service
Routes can be parameterized to match more than one URL:
The value of the parameters are made available on the $routeParams service
.when('/restaurants/:id', { ... });
Add a new view for a single restaurant showing its menu
If a directive declares isolated scope
scope: { ... }
then a new isolated scope is created at that element and the template of the directive is bound to this new scope
You can map attributes on the element to properties on the controller
bindToController: {
user: '=deliverTo'
},
Refactor the delivery info form and display box into a reusable `fmDeliverTo` directive
karma-ng-html2js-preprocessor
Test the `fmDeliverTo` directive
Use `fmDeliverTo` in restaurant and menu views
Provide a Shopping Cart to store items from the menu
Provide a Shopping Cart to store items from the menu
Services are injected based on named parameters:
But if you minimize the code then you lose the name
so you must annotate
or
function localStorageBinding(localStorage, $rootScope) { ... }
function localStorageBinding(a, b) { ... }
['localStorage', '$rootScope', function localStorageBinding(localStorage, $rootScope) { ... }]
localStorageBinding.$inject = ['localStorage', '$rootScope'];
Add injection annotations to prevent minification issues