1- var app = angular . module ( 'app' , [ ] ) ;
1+ var app = angular . module ( 'app' , [ 'starTrekServices' ] ) ;
22
33app . config ( function ( $locationProvider , $routeProvider ) {
44 $routeProvider . when ( "/" , { templateUrl :"partials/home.html" } ) ;
5- $routeProvider . when ( "/cat" , {
6- templateUrl :"partials/cat.html" ,
7- controller :"CatCtrl" ,
8- resolve :{
9- toy :function ( $timeout , $q ) {
10- var defer = $q . defer ( ) ;
11-
12- //delay the response for 2 seconds, then succeed
13- $timeout ( function ( ) {
14- //you can resolve and pass whatever you want here (use a resource, etc)
15- //the result is passed into the "toy" param in the CatCtrl
16- defer . resolve (
17- { type :"yarn" }
18- ) ;
19- } , 2000 ) ;
205
21- return defer . promise ;
22- }
23- }
24-
25- } ) ;
26- $routeProvider . when ( "/dog" , {
27- templateUrl :"partials/dog.html" ,
28- controller :"DogCtrl" ,
6+ $routeProvider . when ( "/:starship" , {
7+ templateUrl :"partials/starship.html" ,
8+ controller :function ( $scope , $routeParams , crew ) {
9+ $scope . img = $routeParams . starship + ".jpg" ;
10+ $scope . crew = crew ;
11+ } ,
2912 resolve :{
30- toy :function ( $timeout , $q ) {
31- var defer = $q . defer ( ) ;
13+ crew :function ( $q , $route , $timeout , starTrekResource ) {
14+ var deferred = $q . defer ( ) ;
15+
16+ var starship = $route . current . params . starship ;
3217
33- //delay the response for 2 seconds, then reject
18+ var successCb = function ( result ) {
19+ if ( angular . equals ( result , [ ] ) ) {
20+ deferred . reject ( "No starship found by that name" ) ;
21+ }
22+ else {
23+ deferred . resolve ( result ) ;
24+ }
25+ } ;
26+ //the timeout is only to exaggerate the example, it's completely unnecessary
3427 $timeout ( function ( ) {
35- //the reject message is only useful for the $routeChangeError rejection
36- defer . reject ( "Dogs don't deserve toys!" ) ;
28+ starTrekResource . getCrewByStarship ( starship , successCb ) ;
3729 } , 2000 ) ;
3830
39- return defer . promise ;
31+ return deferred . promise ;
4032 }
4133 }
42- } ) ;
4334
44- //Even silly demo apps need Easter Eggs :)
45- $routeProvider . when ( "/aquaman" , { templateUrl :"partials/aquaman.html" } ) ;
35+ } ) ;
4636} ) ;
4737
48- function CatCtrl ( $scope , toy ) {
49- //notice that this is triggered AFTER the route has successfull changed
50- //this means you can prepare any data in the "toy" that you want
51- alert ( "CatCtrl ready - The cat likes: " + toy . type ) ;
52- }
53- function DogCtrl ( $scope , toy ) {
54- //this will never happen since we're intentionally failing the route change
55- alert ( "DogCtrl ready - The dog likes: " + toy . type ) ;
56- }
57-
5838function AppCtrl ( $scope , $rootScope , $location ) {
5939 $rootScope . $on ( "$routeChangeStart" , function ( event , next , current ) {
60- $scope . alertType = "alert-info " ;
61- $scope . alertMessage = "Changing routes " ;
62- $scope . active = "progress-striped active" ;
40+ $scope . alertType = "" ;
41+ $scope . alertMessage = "Loading... " ;
42+ $scope . active = "progress-striped active progress-warning " ;
6343 } ) ;
6444 $rootScope . $on ( "$routeChangeSuccess" , function ( event , current , previous ) {
6545 $scope . alertType = "alert-success" ;
6646 $scope . alertMessage = "Successfully changed routes :)" ;
67- $scope . active = "" ;
47+ $scope . active = "progress-success " ;
6848 } ) ;
6949 $rootScope . $on ( "$routeChangeError" , function ( event , current , previous , rejection ) {
70- alert ( rejection ) ;
50+ alert ( "ROUTE CHANGE ERROR: " + rejection ) ;
7151 $scope . alertType = "alert-error" ;
7252 $scope . alertMessage = "Failed to change routes :(" ;
7353 $scope . active = "" ;
@@ -78,16 +58,16 @@ function AppCtrl($scope, $rootScope, $location) {
7858
7959 $scope . tabs = [
8060 {
81- title :"Home - No Resolve " ,
61+ title :"Home" ,
8262 url :"#/"
8363 } ,
8464 {
85- title :"Cats Succeed " ,
86- url :"#/cat "
65+ title :"Enterprise-D " ,
66+ url :"#/Enterprise-D "
8767 } ,
8868 {
89- title :"Dogs Fail " ,
90- url :"#/dog "
69+ title :"Voyager " ,
70+ url :"#/Voyager "
9171 }
9272 ] ;
9373
0 commit comments