diff --git a/index.html b/index.html index 1de411a..82f1f27 100644 --- a/index.html +++ b/index.html @@ -6,6 +6,7 @@ +
diff --git a/scripts/app.js b/scripts/app.js index cf4cd16..60b573c 100644 --- a/scripts/app.js +++ b/scripts/app.js @@ -1,6 +1,6 @@ 'use strict'; -angular +var app = angular .module('ngdemoApp', [ 'ngRoute' ]) diff --git a/scripts/controllers/mainCtrl.js b/scripts/controllers/mainCtrl.js index 005e365..bfa46d3 100644 --- a/scripts/controllers/mainCtrl.js +++ b/scripts/controllers/mainCtrl.js @@ -1,12 +1,15 @@ 'use strict'; angular.module('ngdemoApp') - .controller('MainCtrl', function ($scope) { + .controller('MainCtrl', function ($scope, BookService) { - // some data for the table - $scope.books = [ - {author: 'J.R.R. Tolkien', title: 'The Hobbit'}, - {author: 'Douglas Adams', title: "Hitchhiker's Guide to the Galaxy"}, - {author: 'Herman Melville', title: 'Moby Dick'}, - ]; + // a simple filter object, with default filter values + $scope.filters = { author: '', title: '' }; + + $scope.update = function() { + BookService.all($scope.filters).then(function(data) { + $scope.books = data; + }); + } + $scope.update(); }); diff --git a/scripts/services/bookService.js b/scripts/services/bookService.js new file mode 100644 index 0000000..0d67911 --- /dev/null +++ b/scripts/services/bookService.js @@ -0,0 +1,34 @@ +'use strict'; +app.service('BookService', function ($q) { + + // some data for the table + this.books = [ + {author: 'J.R.R. Tolkien', title: 'The Fellowship of the Ring'}, + {author: 'J.R.R. Tolkien', title: 'The Two Towers'}, + {author: 'J.R.R. Tolkien', title: 'The Return of the King'}, + {author: 'Douglas Adams', title: "Hitchhiker's Guide to the Galaxy"}, + {author: 'Herman Melville', title: 'Moby Dick'}, + ]; + + this.all = function (filterObject) { + + // This service acts like it called $http.get and returned the resulting + // proimse. Really it just builds a promise around some mock data. + var defer = $q.defer(); + + var result = []; + for (var i = 0; i < this.books.length; i++) { + if (filterObject.author != '' && this.books[i].author.toLowerCase().indexOf(filterObject.author.toLowerCase()) == -1) { + continue; + } + if (filterObject.title != '' && this.books[i].title.toLowerCase().indexOf(filterObject.title.toLowerCase()) == -1) { + continue; + } + result.push(this.books[i]); + } + defer.resolve(result); + + return defer.promise; + }; + +}); diff --git a/views/main.html b/views/main.html index 5a877f4..37bae73 100644 --- a/views/main.html +++ b/views/main.html @@ -4,6 +4,10 @@