Skip to content

Commit 49b80ac

Browse files
committed
Fixes for john's comments
1 parent a1530b3 commit 49b80ac

File tree

8 files changed

+52
-50
lines changed

8 files changed

+52
-50
lines changed

chapter4/guthub/app/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
<title>GutHub - Create and Share</title>
55
<script src="scripts/vendor/angular.min.js"></script>
66
<script src="scripts/vendor/angular-resource.min.js"></script>
7-
<script src="scripts/vendor/showdown.js"></script>
87
<script src="scripts/directives/directives.js"></script>
98
<script src="scripts/services/services.js"></script>
109
<script src="scripts/controllers/controllers.js"></script>

chapter4/guthub/app/scripts/controllers/controllers.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,56 @@
11
'use strict';
22

3-
var app = angular.module('guthub', ['directives', 'services']);
3+
var app = angular.module('guthub',
4+
['guthub.directives', 'guthub.services']);
45

56
app.config(['$routeProvider', function($routeProvider) {
67
$routeProvider.
78
when('/', {
89
controller: 'ListCtrl',
910
resolve: {
10-
recipes: function(RecipesLoader) {
11-
return RecipesLoader();
11+
recipes: function(MultiRecipeLoader) {
12+
return MultiRecipeLoader();
1213
}
1314
},
1415
templateUrl:'/views/list.html'
1516
}).when('/edit/:recipeId', {
16-
controller: 'EditRecipeCtrl',
17+
controller: 'EditCtrl',
1718
resolve: {
1819
recipe: function(RecipeLoader) {
1920
return RecipeLoader();
2021
}
2122
},
2223
templateUrl:'/views/recipeForm.html'
2324
}).when('/view/:recipeId', {
24-
controller: 'ViewRecipeCtrl',
25+
controller: 'ViewCtrl',
2526
resolve: {
2627
recipe: function(RecipeLoader) {
2728
return RecipeLoader();
2829
}
2930
},
3031
templateUrl:'/views/viewRecipe.html'
3132
}).when('/new', {
32-
controller: 'NewRecipeCtrl',
33+
controller: 'NewCtrl',
3334
templateUrl:'/views/recipeForm.html'
3435
}).otherwise({redirectTo:'/'});
3536
}]);
3637

37-
app.controller('ListCtrl', ['$scope', 'recipes', function($scope, recipes) {
38+
app.controller('ListCtrl', ['$scope', 'recipes',
39+
function($scope, recipes) {
3840
$scope.recipes = recipes;
3941
}]);
4042

41-
app.controller('ViewRecipeCtrl', ['$scope', '$location', 'recipe', function($scope, $location, recipe) {
43+
app.controller('ViewCtrl', ['$scope', '$location', 'recipe',
44+
function($scope, $location, recipe) {
4245
$scope.recipe = recipe;
4346

4447
$scope.edit = function() {
4548
$location.path('/edit/' + recipe.id);
4649
};
4750
}]);
4851

49-
app.controller('EditRecipeCtrl', ['$scope', '$location', 'recipe', function($scope, $location, recipe) {
52+
app.controller('EditCtrl', ['$scope', '$location', 'recipe',
53+
function($scope, $location, recipe) {
5054
$scope.recipe = recipe;
5155

5256
$scope.save = function() {
@@ -61,8 +65,11 @@ app.controller('EditRecipeCtrl', ['$scope', '$location', 'recipe', function($sco
6165
};
6266
}]);
6367

64-
app.controller('NewRecipeCtrl', ['$scope', '$location', 'Recipe', function($scope, $location, Recipe) {
65-
$scope.recipe = new Recipe({ingredients:[{}]});
68+
app.controller('NewCtrl', ['$scope', '$location', 'Recipe',
69+
function($scope, $location, Recipe) {
70+
$scope.recipe = new Recipe({
71+
ingredients: [ {} ]
72+
});
6673

6774
$scope.save = function() {
6875
$scope.recipe.$save(function(recipe) {
@@ -71,7 +78,8 @@ app.controller('NewRecipeCtrl', ['$scope', '$location', 'Recipe', function($scop
7178
};
7279
}]);
7380

74-
app.controller('IngredientsCtrl', ['$scope', function($scope) {
81+
app.controller('IngredientsCtrl', ['$scope',
82+
function($scope) {
7583
$scope.addIngredient = function() {
7684
var ingredients = $scope.recipe.ingredients;
7785
ingredients[ingredients.length] = {};

chapter4/guthub/app/scripts/directives/directives.js

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
11
'use strict';
22

3-
var directives = angular.module('directives', []);
3+
var directives = angular.module('guthub.directives', []);
44

5-
directives.directive('markdown', function() {
6-
var showdown = new Showdown.converter();
7-
return {
8-
restrict: 'E',
9-
scope: {content: '&content'},
10-
link: function(scope, element, attrs) {
11-
scope.$watch(scope.content, function(value) {
12-
if (value) {
13-
var htmlText = showdown.makeHtml(value);
14-
element.html(htmlText);
15-
}
16-
});
17-
}
18-
};
19-
});
20-
21-
directives.directive('butterbar', ['$rootScope', function($rootScope) {
5+
directives.directive('butterbar', ['$rootScope',
6+
function($rootScope) {
227
return {
238
link: function(scope, element, attrs) {
249
element.addClass('hide');
@@ -34,7 +19,8 @@ directives.directive('butterbar', ['$rootScope', function($rootScope) {
3419
};
3520
}]);
3621

37-
directives.directive('focus', function() {
22+
directives.directive('focus',
23+
function() {
3824
return {
3925
link: function(scope, element, attrs) {
4026
element[0].focus();

chapter4/guthub/app/scripts/services/services.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
'use strict';
22

3-
var services = angular.module('services', ['ngResource']);
3+
var services = angular.module('guthub.services',
4+
['ngResource']);
45

5-
services.factory('Recipe', ['$resource', function($resource) {
6+
services.factory('Recipe', ['$resource',
7+
function($resource) {
68
return $resource('/recipes/:id', {id: '@id'});
79
}]);
810

9-
services.factory('RecipesLoader', ['Recipe', '$q', function(Recipe, $q) {
11+
services.factory('MultiRecipeLoader', ['Recipe', '$q',
12+
function(Recipe, $q) {
1013
return function() {
1114
var delay = $q.defer();
1215
Recipe.query(function(recipes) {
@@ -18,7 +21,8 @@ services.factory('RecipesLoader', ['Recipe', '$q', function(Recipe, $q) {
1821
};
1922
}]);
2023

21-
services.factory('RecipeLoader', ['Recipe', '$route', '$q', function(Recipe, $route, $q) {
24+
services.factory('RecipeLoader', ['Recipe', '$route', '$q',
25+
function(Recipe, $route, $q) {
2226
return function() {
2327
var delay = $q.defer();
2428
Recipe.get({id: $route.current.params.recipeId}, function(recipe) {

chapter4/guthub/app/views/recipeForm.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ <h2>Edit Recipe</h2>
33
<div class="control-group">
44
<label class="control-label" for="title">Title:</label>
55
<div class="controls">
6-
<input ng-model="recipe.title" class="input-xlarge" id="title">
6+
<input ng-model="recipe.title" class="input-xlarge" id="title" focus>
77
</div>
88
</div>
99

chapter4/guthub/app/views/viewRecipe.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ <h2>{{recipe.title}}</h2>
22

33
<div>{{recipe.description}}</div>
44

5+
<h3>Ingredients</h3>
56
<ul class="unstyled">
67
<li ng-repeat="ingredient in recipe.ingredients">
78
<span>{{ingredient.amount}}</span>
@@ -10,7 +11,8 @@ <h2>{{recipe.title}}</h2>
1011
</li>
1112
</ul>
1213

13-
<markdown content="recipe.instructions"></markdown>
14+
<h3>Instructions</h3>
15+
<div>{{recipe.instructions}}</div>
1416

1517
<form ng-submit="edit()" class="form-horizontal">
1618
<div class="form-actions">

chapter4/guthub/test/spec/controllersSpec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ describe('Controllers', function() {
2929
});
3030
});
3131

32-
describe('RecipesLoader', function() {
32+
describe('MultiRecipeLoader', function() {
3333
var mockBackend, recipe, loader;
3434
// The _$httpBackend_ is the same as $httpBackend. Only written this way to
3535
// differentiate between injected variables and local variables
36-
beforeEach(inject(function(_$httpBackend_, Recipe, RecipesLoader) {
36+
beforeEach(inject(function(_$httpBackend_, Recipe, MultiRecipeLoader) {
3737
recipe = Recipe;
3838
mockBackend = _$httpBackend_;
39-
loader = RecipesLoader;
39+
loader = MultiRecipeLoader;
4040
}));
4141

4242
it('should load list of recipes', function() {
@@ -64,7 +64,7 @@ describe('Controllers', function() {
6464
location = $location;
6565
$scope = $rootScope.$new();
6666

67-
ctrl = $controller('EditRecipeCtrl', {
67+
ctrl = $controller('EditCtrl', {
6868
$scope: $scope,
6969
$location: $location,
7070
recipe: new Recipe({id: 1, title: 'Recipe'})

chapter4/guthub/web-server.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ app.configure(function(){
1111

1212
var recipes_map = {
1313
'1': {
14-
id: 1,
15-
'title': 'Recipe 1',
16-
'description': 'Description 1',
17-
'instructions': 'Instruction 1',
18-
ingredients: [
19-
{amount: 3, amountUnits: 'pounds', ingredientName: 'Awesomeness'},
20-
{amount: 5, amountUnits: 'stuff', ingredientName: 'Good stuff'}
21-
]
14+
"id": "1",
15+
"title": "Cookies",
16+
"description": "Delicious, crisp on the outside, chewy on the outside, oozing with chocolatey goodness cookies. The best kind",
17+
"ingredients": [
18+
{
19+
"amount": "1",
20+
"amountUnits": "packet",
21+
"ingredientName": "Chips Ahoy"
22+
}
23+
],
24+
"instructions": "1. Go buy a packet of Chips Ahoy\n2. Heat it up in an oven\n3. Enjoy warm cookies\n4. Learn how to bake cookies from somewhere else"
2225
},
2326
'2': {
2427
id: 2,

0 commit comments

Comments
 (0)