16.07.2017 Views

AngularJS Essentials

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Modules<br />

},<br />

template:<br />

"" +<br />

"{{title}}" +<br />

"" +<br />

"" +<br />

""<br />

};<br />

});<br />

Great! Now we are ready to pack our library inside one script file. For this, again,<br />

we may rely on Grunt, through the grunt-contrib-concat plugin, for creating this<br />

concatenation for us. The destination file in this case would be ui.js, and we are<br />

going to declare it inside the index.html file of our parking application.<br />

The search module<br />

The search module will contain carSearchService, which we created in Chapter 4,<br />

Dependency Injection and Services.<br />

Again, we are going to start by declaring the module search in the app.js file,<br />

as follows:<br />

var search = angular.module("search", []);<br />

Because we want to deliver this service as a reusable component, it would be nice to<br />

get rid of the car concept, making it more generic. To do that, let's just change it from<br />

car to entity. Consider the following code snippet in the searchService.js file:<br />

search.factory('searchService', function ($timeout, $q) {<br />

var _filter = function (entities, criteria) {<br />

var deferred = $q.defer();<br />

$timeout(function () {<br />

var result = [];<br />

angular.forEach(entities, function (entity) {<br />

if (_matches(entity, criteria)) {<br />

result.push(entity);<br />

}<br />

});<br />

if (result.length > 0) {<br />

[ 118 ]<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!