Skip to content

Commit 2840efd

Browse files
committed
Improving the example to make it more obvious what's happening
1 parent 0f55f0a commit 2840efd

File tree

14 files changed

+232
-158
lines changed

14 files changed

+232
-158
lines changed

.idea/workspace.xml

Lines changed: 163 additions & 95 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/img/Enterprise-D.jpg

11.3 KB
Loading

client/img/Voyager.jpg

4.13 KB
Loading

client/img/aquaman.jpg

-5.19 KB
Binary file not shown.

client/img/cat.jpg

-4.18 KB
Binary file not shown.

client/img/dog.jpg

-11.4 KB
Binary file not shown.

client/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
<a href="https://github.com/johnlindquist/johnlindquist.github.com/tree/master/demos/angular-resolve">View source on Github</a>
2626

2727
<script type="text/javascript" src="lib/angular-1.0.2/angular.js"></script>
28+
<script type="text/javascript" src="lib/angular-1.0.2/angular-resource.js"></script>
2829
<script type="text/javascript" src="lib/jquery-1.8.1/jquery-1.8.1.js"></script>
2930
<script type="text/javascript" src="lib/bootstrap-2.1.1/js/bootstrap.js"></script>
3031
<script type="text/javascript" src="js/app.js"></script>
32+
<script type="text/javascript" src="js/starTrekServices.js"></script>
3133
</body>
3234
</html>

client/js/app.js

Lines changed: 33 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,53 @@
1-
var app = angular.module('app', []);
1+
var app = angular.module('app', ['starTrekServices']);
22

33
app.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-
5838
function 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

client/js/starTrekServices.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
angular.module('starTrekServices', ['ngResource']).
2+
factory('starTrekResource', function ($resource) {
3+
var StarTrekResource = $resource('https://api.mongolab.com/api/1/databases' +
4+
'/johnlindquist/collections/star-trek/',
5+
{apiKey:'4f0f9e96e4b04ac27016b99a'},
6+
{monogolabQuery:{method:'GET', params:{q:""}, isArray:true}});
7+
8+
9+
StarTrekResource.prototype.getCrewByStarship = function (starshipQuery, successCb, failCb) {
10+
var queryObj = {starship:starshipQuery};
11+
var query = JSON.stringify(queryObj);
12+
return StarTrekResource.monogolabQuery({q:query}, successCb, failCb);
13+
};
14+
15+
return new StarTrekResource;
16+
});

client/partials/aquaman.html

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)