diff --git a/app/app.js b/app/app.js index f176306..8e3a637 100644 --- a/app/app.js +++ b/app/app.js @@ -1,4 +1,22 @@ -var app = angular.module('app', ['ngRoute']); +var app = angular.module('app', ['ngRoute', 'ngResource']); // Root controllers in /root/ folder // Page controllers in /pages/[your-page] folder + +// Factories +app.factory('Foods', function($resource) { + return $resource('assets/json/foods.json'); +}); + +// Just does a PUT request when u call Food.update +app.factory('Food', function ($resource) { + // {food: '@id'} means replace :food with $scope.foodCRUD.id + var data = $resource('http://foodbank.develop.digitalmediauconn.org/api/food/:food', {food: '@id'}, { + update:{ method:'PUT' } + }); + return data; +}); + +app.factory('Categories', function($resource) { + return $resource('assets/json/categories.json'); +}); diff --git a/app/app.route.js b/app/app.route.js index 2dbcd17..a71574b 100644 --- a/app/app.route.js +++ b/app/app.route.js @@ -21,21 +21,9 @@ app.config( function ($routeProvider, $locationProvider) { }) // Sandbox Demo - .when('/news', { - templateUrl: 'app/sandbox/news.html', - controller: 'NewsCtrl' - }) - - // Sandbox Demo - .when('/competitions', { - templateUrl: 'app/sandbox/competition.html', - controller: 'CompetitionCtrl' - }) - - // Sandbox Demo - .when('/todo', { - templateUrl: 'app/sandbox/todo.html', - controller: 'TodoCtrl' + .when('/rest', { + templateUrl: 'app/sandbox/rest.html', + controller: 'RestCtrl' }) // 404 Not Found diff --git a/app/pages/food/ctrl.js b/app/pages/food/ctrl.js index 73b16ee..e683268 100644 --- a/app/pages/food/ctrl.js +++ b/app/pages/food/ctrl.js @@ -1,4 +1,4 @@ -app.controller('FoodCtrl', ['$scope', '$http', function ($scope, $http) { +app.controller('FoodCtrl', ['$scope', '$http', 'Foods', 'Food', 'Categories', function ($scope, $http, Foods, Food, Categories) { $scope.page.title = 'Food'; $scope.page.id = 'food'; @@ -14,6 +14,8 @@ app.controller('FoodCtrl', ['$scope', '$http', function ($scope, $http) { $scope.categorySelection = { "all": true }; $scope.searchTerm = ""; + + // Initialize CRUD modes $scope.editMode = false; $scope.createMode = false; @@ -22,44 +24,42 @@ app.controller('FoodCtrl', ['$scope', '$http', function ($scope, $http) { $scope.foodCRUD = {}; // GET foods - $http.get("assets/json/foods.json") - .then(function(response) { - $scope.foods = response.data.data; + Foods.get({}, function (data) { + $scope.foods = data.data; }); // GET list of categories - $http.get("assets/json/categories.json") - .then(function(response) { - var categories = response.data.data; + Categories.get({}, function (data) { + var categories = data.data; categories.forEach(function(cat){ $scope.categories[cat.id] = cat; }); $scope.categories.forEach(function(c){ $scope.categorySelection[c.id] = false; }); - // Preset category filters $scope.resetCategorySelection(); }); + + // Set category filters to false $scope.resetCategorySelection = function () { $scope.categories.forEach(function(c){ $scope.categorySelection[c.id] = false; }); - } - // Category filter function $scope.categoryFilter = function (food) { return ( $scope.categorySelection[food.category_id] | $scope.categorySelection["all"] ); } - // Rank filter function $scope.rankFilter = function (food) { return ( $scope.rankSelection[food.rank_id] ); } + + // CRUD: edit food $scope.edit = function (foodToEdit) { $scope.editMode = true; @@ -86,14 +86,7 @@ app.controller('FoodCtrl', ['$scope', '$http', function ($scope, $http) { // CRUD: POST $scope.submit = function () { $scope.cancel(); // hide modal - - $http.post('request-url', $scope.foodCRUD) - .success(function (data) { - // console.log(data); - }) - .error(function (data) { - // console.log(data); - }); + Food.update($scope.foodCRUD); }; diff --git a/app/root/_header.kit b/app/root/_header.kit index 86bd8c6..be12df1 100644 --- a/app/root/_header.kit +++ b/app/root/_header.kit @@ -12,6 +12,7 @@ + +
+ {{key}} : {{value}} +
diff --git a/assets/js/bundle.js b/assets/js/bundle.js index 98ca30d..3f05148 100644 --- a/assets/js/bundle.js +++ b/assets/js/bundle.js @@ -1,8 +1,26 @@ -var app = angular.module('app', ['ngRoute']); +var app = angular.module('app', ['ngRoute', 'ngResource']); // Root controllers in /root/ folder // Page controllers in /pages/[your-page] folder +// Factories +app.factory('Foods', function($resource) { + return $resource('assets/json/foods.json'); +}); + +// Just does a PUT request when u call Food.update +app.factory('Food', function ($resource) { + // {food: '@id'} means replace :food with $scope.foodCRUD.id + var data = $resource('http://foodbank.develop.digitalmediauconn.org/api/food/:food', {food: '@id'}, { + update:{ method:'PUT' } + }); + return data; +}); + +app.factory('Categories', function($resource) { + return $resource('assets/json/categories.json'); +}); + app.config( function ($routeProvider, $locationProvider) { $routeProvider .when('/', { @@ -26,21 +44,9 @@ app.config( function ($routeProvider, $locationProvider) { }) // Sandbox Demo - .when('/news', { - templateUrl: 'app/sandbox/news.html', - controller: 'NewsCtrl' - }) - - // Sandbox Demo - .when('/competitions', { - templateUrl: 'app/sandbox/competition.html', - controller: 'CompetitionCtrl' - }) - - // Sandbox Demo - .when('/todo', { - templateUrl: 'app/sandbox/todo.html', - controller: 'TodoCtrl' + .when('/rest', { + templateUrl: 'app/sandbox/rest.html', + controller: 'RestCtrl' }) // 404 Not Found @@ -71,6 +77,23 @@ app.controller('root', ['$scope', '$location', function ($scope, $location) { }; }]); +app.controller('RestCtrl', ['$scope', 'Foods', function ($scope, Foods) { + $scope.page.title = 'REST'; + $scope.page.id = 'rest'; + $scope.page.blackNav = false; + + Foods.get({}, function (data) { + $scope.foods = data.data; + }); +}]); + + + + + + + + app.controller('NewsCtrl', ['$scope', function ($scope) { $scope.page.title = 'News'; $scope.page.id = 'news'; @@ -99,15 +122,6 @@ app.controller('EmailController', ['$scope', function ($scope) { $scope.email = "rogangabe@gmail.com"; }]); - -app.controller('CategoryCtrl', ['$scope', function ($scope) { - $scope.page.title = 'Categories'; - $scope.page.id = 'cat'; - $scope.page.yourVar = "globalStuff"; - - $scope.var2 = "localStuff"; -}]); - app.controller('404Ctrl', ['$scope', function ($scope) { $scope.page.title = '404'; $scope.page.id = '404'; @@ -116,7 +130,7 @@ app.controller('404Ctrl', ['$scope', function ($scope) { $scope.var2 = "localStuff"; }]); -app.controller('FoodCtrl', ['$scope', '$http', function ($scope, $http) { +app.controller('FoodCtrl', ['$scope', '$http', 'Foods', 'Food', 'Categories', function ($scope, $http, Foods, Food, Categories) { $scope.page.title = 'Food'; $scope.page.id = 'food'; @@ -132,6 +146,8 @@ app.controller('FoodCtrl', ['$scope', '$http', function ($scope, $http) { $scope.categorySelection = { "all": true }; $scope.searchTerm = ""; + + // Initialize CRUD modes $scope.editMode = false; $scope.createMode = false; @@ -140,44 +156,42 @@ app.controller('FoodCtrl', ['$scope', '$http', function ($scope, $http) { $scope.foodCRUD = {}; // GET foods - $http.get("assets/json/foods.json") - .then(function(response) { - $scope.foods = response.data.data; + Foods.get({}, function (data) { + $scope.foods = data.data; }); // GET list of categories - $http.get("assets/json/categories.json") - .then(function(response) { - var categories = response.data.data; + Categories.get({}, function (data) { + var categories = data.data; categories.forEach(function(cat){ $scope.categories[cat.id] = cat; }); $scope.categories.forEach(function(c){ $scope.categorySelection[c.id] = false; }); - // Preset category filters $scope.resetCategorySelection(); }); + + // Set category filters to false $scope.resetCategorySelection = function () { $scope.categories.forEach(function(c){ $scope.categorySelection[c.id] = false; }); - } - // Category filter function $scope.categoryFilter = function (food) { return ( $scope.categorySelection[food.category_id] | $scope.categorySelection["all"] ); } - // Rank filter function $scope.rankFilter = function (food) { return ( $scope.rankSelection[food.rank_id] ); } + + // CRUD: edit food $scope.edit = function (foodToEdit) { $scope.editMode = true; @@ -204,20 +218,22 @@ app.controller('FoodCtrl', ['$scope', '$http', function ($scope, $http) { // CRUD: POST $scope.submit = function () { $scope.cancel(); // hide modal - - $http.post('request-url', $scope.foodCRUD) - .success(function (data) { - // console.log(data); - }) - .error(function (data) { - // console.log(data); - }); + Food.update($scope.foodCRUD); }; }]); +app.controller('CategoryCtrl', ['$scope', function ($scope) { + $scope.page.title = 'Categories'; + $scope.page.id = 'cat'; + $scope.page.yourVar = "globalStuff"; + + $scope.var2 = "localStuff"; +}]); + + app.controller('LoginCtrl', ['$scope', 'loginService', function ($scope, loginService) { $scope.page.title = 'Login'; $scope.page.id = 'login'; diff --git a/index.html b/index.html index 3f076d0..a5c9c52 100644 --- a/index.html +++ b/index.html @@ -14,6 +14,7 @@ +