This repository has been archived by the owner on Jan 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create BookService to handle the data retrieval and filtering
- Loading branch information
Showing
5 changed files
with
50 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
'use strict'; | ||
|
||
angular | ||
var app = angular | ||
.module('ngdemoApp', [ | ||
'ngRoute' | ||
]) | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; | ||
|
||
}); |
This file contains 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