From 625707ecfc678e6e1e31f0f90beaa51389cd2ffe Mon Sep 17 00:00:00 2001 From: Steven Cassidy Date: Thu, 7 Jan 2016 20:52:25 -0500 Subject: [PATCH 1/5] Add a cat model object --- index.html | 8 +++++--- js/controllers.js | 8 ++++---- js/factories.js | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 js/factories.js diff --git a/index.html b/index.html index f0d4f80..cb113eb 100644 --- a/index.html +++ b/index.html @@ -5,6 +5,7 @@ + @@ -30,16 +31,17 @@

My kittens

Filter (on breed / name):
- +
-
Name:{{cat.id}} {{cat.name}}
+
Name:{{cat.name}}
Breed: {{cat.breed_name}}
-
Born: {{cat.born_on}}
+
Born on: {{cat.bornOn | date : 'dd-MMM-yyyy'}}
+
Age: {{cat.getAge()}}
diff --git a/js/controllers.js b/js/controllers.js index 549482f..3c453cb 100644 --- a/js/controllers.js +++ b/js/controllers.js @@ -1,14 +1,14 @@ angular.module('steven').controller('MainController', - ['$http', '$filter', '$scope', function($http, $filter, $scope){ + ['Cat', '$http', '$filter', '$scope', function(Cat, $http, $filter, $scope){ $http.get('https://stark-harbor-5038.herokuapp.com/breeds') .then(function(response){ $scope.breeds = response.data; }); - $http.get('https://stark-harbor-5038.herokuapp.com/cats') - .then(function(response){ - $scope.allCats = response.data; + Cat.getCats() + .then(function(cats){ + $scope.allCats = cats; $scope.showMatches(); }); diff --git a/js/factories.js b/js/factories.js new file mode 100644 index 0000000..f565bae --- /dev/null +++ b/js/factories.js @@ -0,0 +1,35 @@ +angular.module('steven').factory('Cat', function($http) { + + var apiUrl = 'https://stark-harbor-5038.herokuapp.com/cats'; + + var Cat = function(json) { + angular.extend(this, json); + this.bornOn = new Date(json.born_on); + this.createdAt = new Date(json.created_at); + this.updatedAt = new Date(json.updted_at); + }; + + Cat.prototype.getAge = function() { + var today = new Date(); + var age = today.getFullYear() - this.bornOn.getFullYear(); + var m = today.getMonth() - this.bornOn.getMonth(); + if (m < 0 || (m === 0 && today.getDate() < this.bornOn.getDate())) { + age--; + } + return age; + }; + + Cat.getCats = function() { + return $http.get('https://stark-harbor-5038.herokuapp.com/cats') + .then(function(response){ + var result = []; + angular.forEach(response.data, function(json){ + result.push(new Cat(json)); + }); + return result; + }); + }; + + + return Cat; +}); From c95cf27651e4c63ff5bbb277a80e060248963e70 Mon Sep 17 00:00:00 2001 From: Steven Cassidy Date: Thu, 7 Jan 2016 22:06:36 -0500 Subject: [PATCH 2/5] Use Cat to create cats --- js/controllers.js | 5 ++--- js/factories.js | 7 +++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/js/controllers.js b/js/controllers.js index 3c453cb..8edf2e8 100644 --- a/js/controllers.js +++ b/js/controllers.js @@ -19,10 +19,9 @@ angular.module('steven').controller('MainController', }; $scope.addNewCat = function() { - $http.post('https://stark-harbor-5038.herokuapp.com/cats', $scope.newCat) - .then(function(response){ - var savedCat = response.data; + Cat.create($scope.newCat).then(function(savedCat){ $scope.allCats.unshift(savedCat); + $scope.showMatches(); }); }; diff --git a/js/factories.js b/js/factories.js index f565bae..81dc258 100644 --- a/js/factories.js +++ b/js/factories.js @@ -7,6 +7,7 @@ angular.module('steven').factory('Cat', function($http) { this.bornOn = new Date(json.born_on); this.createdAt = new Date(json.created_at); this.updatedAt = new Date(json.updted_at); + console.log(this); }; Cat.prototype.getAge = function() { @@ -30,6 +31,12 @@ angular.module('steven').factory('Cat', function($http) { }); }; + Cat.create = function(catProps) { + return $http.post('https://stark-harbor-5038.herokuapp.com/cats', catProps) + .then(function(response){ + return new Cat(response.data); + }); + }; return Cat; }); From b76615de71d2a38600dee229baed1656456e3f3f Mon Sep 17 00:00:00 2001 From: Steven Cassidy Date: Fri, 8 Jan 2016 09:16:37 -0500 Subject: [PATCH 3/5] Add and use a Breed model --- index.html | 1 + js/controllers.js | 8 ++++---- js/factories/breed.js | 21 +++++++++++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 js/factories/breed.js diff --git a/index.html b/index.html index cb113eb..5d505a3 100644 --- a/index.html +++ b/index.html @@ -5,6 +5,7 @@ + diff --git a/js/controllers.js b/js/controllers.js index 8edf2e8..10cfc9d 100644 --- a/js/controllers.js +++ b/js/controllers.js @@ -1,9 +1,9 @@ angular.module('steven').controller('MainController', - ['Cat', '$http', '$filter', '$scope', function(Cat, $http, $filter, $scope){ + ['Breed', 'Cat', '$filter', '$scope', function(Breed, Cat, $filter, $scope){ - $http.get('https://stark-harbor-5038.herokuapp.com/breeds') - .then(function(response){ - $scope.breeds = response.data; + Breed.getBreeds() + .then(function(breeds){ + $scope.breeds = breeds; }); Cat.getCats() diff --git a/js/factories/breed.js b/js/factories/breed.js new file mode 100644 index 0000000..fc843a5 --- /dev/null +++ b/js/factories/breed.js @@ -0,0 +1,21 @@ +angular.module('steven').factory('Breed', function($http) { + + var apiUrl = 'https://stark-harbor-5038.herokuapp.com/breeds'; + + var Breed = function(json) { + angular.extend(this, json); + }; + + Breed.getBreeds = function() { + return $http.get(apiUrl) + .then(function(response){ + var result = []; + angular.forEach(response.data, function(json){ + result.push(new Breed(json)); + }); + return result; + }); + }; + + return Breed; +}); From a8dee533e5243cadd898c47cd29a6476ccedb99d Mon Sep 17 00:00:00 2001 From: Steven Cassidy Date: Fri, 8 Jan 2016 09:16:51 -0500 Subject: [PATCH 4/5] Remove debug statement --- js/factories.js | 1 - 1 file changed, 1 deletion(-) diff --git a/js/factories.js b/js/factories.js index 81dc258..dd0960b 100644 --- a/js/factories.js +++ b/js/factories.js @@ -7,7 +7,6 @@ angular.module('steven').factory('Cat', function($http) { this.bornOn = new Date(json.born_on); this.createdAt = new Date(json.created_at); this.updatedAt = new Date(json.updted_at); - console.log(this); }; Cat.prototype.getAge = function() { From a25f544ea5415f9f6824605c8a1d673ea034a1d6 Mon Sep 17 00:00:00 2001 From: Steven Cassidy Date: Fri, 8 Jan 2016 09:17:37 -0500 Subject: [PATCH 5/5] Rename the cat factory file --- index.html | 2 +- js/{factories.js => factories/cat.js} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename js/{factories.js => factories/cat.js} (100%) diff --git a/index.html b/index.html index 5d505a3..bd69a38 100644 --- a/index.html +++ b/index.html @@ -6,7 +6,7 @@ - + diff --git a/js/factories.js b/js/factories/cat.js similarity index 100% rename from js/factories.js rename to js/factories/cat.js