forked from shyamseshadri/angularjs-book
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update guthub example and fix coding style
- Loading branch information
1 parent
c0f9fcf
commit 2f5e819
Showing
35 changed files
with
2,373 additions
and
14,895 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<!DOCTYPE html> | ||
<html lang="en" ng-app="guthub"> | ||
<head> | ||
<title>GutHub - Create and Share</title> | ||
<script src="scripts/vendor/angular.min.js"></script> | ||
<script src="scripts/vendor/angular-resource.min.js"></script> | ||
<script src="scripts/vendor/showdown.js"></script> | ||
<script src="scripts/directives/directives.js"></script> | ||
<script src="scripts/services/services.js"></script> | ||
<script src="scripts/controllers/controllers.js"></script> | ||
<link href="styles/bootstrap.css" rel="stylesheet"> | ||
<link href="styles/guthub.css" rel="stylesheet"> | ||
</head> | ||
<body> | ||
<header> | ||
<h1>GutHub</h1> | ||
</header> | ||
|
||
<div butterbar>Loading...</div> | ||
|
||
<div class="container-fluid"> | ||
<div class="row-fluid"> | ||
<div class="span2"> | ||
<!--Sidebar--> | ||
<div id="focus"><a href="/#/new">New Recipe</a></div> | ||
<div><a href="/#/">Recipe List</a></div> | ||
|
||
</div> | ||
<div class="span10"> | ||
<div ng-view></div> | ||
</div> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
'use strict'; | ||
|
||
var app = angular.module('guthub', ['directives', 'services']); | ||
|
||
app.config(['$routeProvider', function($routeProvider) { | ||
$routeProvider. | ||
when('/', { | ||
controller: 'ListCtrl', | ||
resolve: { | ||
recipes: function(RecipesLoader) { | ||
return RecipesLoader(); | ||
} | ||
}, | ||
templateUrl:'/views/list.html' | ||
}).when('/edit/:recipeId', { | ||
controller: 'EditRecipeCtrl', | ||
resolve: { | ||
recipe: function(RecipeLoader) { | ||
return RecipeLoader(); | ||
} | ||
}, | ||
templateUrl:'/views/recipeForm.html' | ||
}).when('/view/:recipeId', { | ||
controller: 'ViewRecipeCtrl', | ||
resolve: { | ||
recipe: function(RecipeLoader) { | ||
return RecipeLoader(); | ||
} | ||
}, | ||
templateUrl:'/views/viewRecipe.html' | ||
}).when('/new', { | ||
controller: 'NewRecipeCtrl', | ||
templateUrl:'/views/recipeForm.html' | ||
}).otherwise({redirectTo:'/'}); | ||
}]); | ||
|
||
app.controller('ListCtrl', ['$scope', 'recipes', function($scope, recipes) { | ||
$scope.recipes = recipes; | ||
}]); | ||
|
||
app.controller('ViewRecipeCtrl', ['$scope', '$location', 'recipe', function($scope, $location, recipe) { | ||
$scope.recipe = recipe; | ||
|
||
$scope.edit = function() { | ||
$location.path('/edit/' + recipe.id); | ||
}; | ||
}]); | ||
|
||
app.controller('EditRecipeCtrl', ['$scope', '$location', 'recipe', function($scope, $location, recipe) { | ||
$scope.recipe = recipe; | ||
|
||
$scope.save = function() { | ||
$scope.recipe.$save(function(recipe) { | ||
$location.path('/view/' + recipe.id); | ||
}); | ||
}; | ||
|
||
$scope.remove = function() { | ||
delete $scope.recipe; | ||
$location.path('/'); | ||
}; | ||
}]); | ||
|
||
app.controller('NewRecipeCtrl', ['$scope', '$location', 'Recipe', function($scope, $location, Recipe) { | ||
$scope.recipe = new Recipe({ingredients:[{}]}); | ||
|
||
$scope.save = function() { | ||
$scope.recipe.$save(function(recipe) { | ||
$location.path('/view/' + recipe.id); | ||
}); | ||
}; | ||
}]); | ||
|
||
app.controller('IngredientsCtrl', ['$scope', function($scope) { | ||
$scope.addIngredient = function() { | ||
var ingredients = $scope.recipe.ingredients; | ||
ingredients[ingredients.length] = {}; | ||
}; | ||
$scope.removeIngredient = function(index) { | ||
$scope.recipe.ingredients.splice(index, 1); | ||
}; | ||
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
var services = angular.module('services', ['ngResource']); | ||
|
||
services.factory('Recipe', ['$resource', function($resource) { | ||
return $resource('/recipes/:id', {id: '@id'}); | ||
}]); | ||
|
||
services.factory('RecipesLoader', ['Recipe', '$q', function(Recipe, $q) { | ||
return function() { | ||
var delay = $q.defer(); | ||
Recipe.query(function(recipes) { | ||
delay.resolve(recipes); | ||
}, function() { | ||
delay.reject('Unable to fetch recipes'); | ||
}); | ||
return delay.promise; | ||
}; | ||
}]); | ||
|
||
services.factory('RecipeLoader', ['Recipe', '$route', '$q', function(Recipe, $route, $q) { | ||
return function() { | ||
var delay = $q.defer(); | ||
Recipe.get({id: $route.current.params.recipeId}, function(recipe) { | ||
delay.resolve(recipe); | ||
}, function() { | ||
delay.reject('Unable to fetch recipe ' + $route.current.params.recipeId); | ||
}); | ||
return delay.promise; | ||
}; | ||
}]); |
10 changes: 10 additions & 0 deletions
10
chapter4/guthub/app/scripts/vendor/angular-resource.min.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.