Created
January 22, 2014 03:39
-
-
Save daniel-nelson/8553133 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.RecipesCtrl = function($scope, $location, Recipe, Ingredient) { | |
$scope.recipes = Recipe.index({}); | |
$scope.recipe = null; | |
$scope.recipeFormVisible = false; | |
$scope.existing_ingredients = []; | |
Ingredient.index(function(ingredients) { | |
return $scope.existing_ingredients = _(ingredients).map(function(ingredient) { | |
return ingredient.name; | |
}); | |
}); | |
$scope.save = function() { | |
if ($scope.recipe.unsavedRecipe) { | |
$scope.recipe = new Recipe($scope.recipe); | |
return $scope.recipe.$create({}, function() { | |
if ($scope.recipe.errors == null) { | |
$scope.hideRecipeForm(); | |
return $scope.recipes.push($scope.recipe); | |
} | |
}); | |
} else { | |
return $scope.recipe.$update({ | |
id: $scope.recipe.id | |
}, function() { | |
if ($scope.recipe.errors == null) { | |
return $scope.hideRecipeForm(); | |
} | |
}); | |
} | |
}; | |
$scope.edit = function(recipe) { | |
$scope.recipe = recipe; | |
return $scope.showRecipeForm(); | |
}; | |
$scope.newRecipe = function() { | |
$scope.recipe = { | |
unsavedRecipe: true | |
}; | |
return $scope.showRecipeForm(); | |
}; | |
$scope.showRecipeForm = function() { | |
$scope.ingredients = Ingredient.index({}); | |
return $scope.recipeFormVisible = true; | |
}; | |
return $scope.hideRecipeForm = function() { | |
return $scope.recipeFormVisible = false; | |
}; | |
}; | |
this.RecipesCtrl.$inject = ['$scope', '$location', 'Recipe', 'Ingredient']; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment