Skip to content

Instantly share code, notes, and snippets.

@daniel-nelson
Created January 22, 2014 03:39
Show Gist options
  • Save daniel-nelson/8553133 to your computer and use it in GitHub Desktop.
Save daniel-nelson/8553133 to your computer and use it in GitHub Desktop.
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