PageRenderTime 1ms CodeModel.GetById 3ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/app/services.js

https://github.com/bartolini/angularjs
JavaScript | 24 lines | 14 code | 3 blank | 7 comment | 0 complexity | 6681f7e20e6cd8bdc3a86850b0f57c54 MD5 | raw file
 1// this is our service module depending on "ngResource" module:
 2services_module = angular.module('searchServices', ['ngResource']);
 3
 4// and here's the definition of "duckDuckGoService":
 5services_module.factory( // again the definitio resembles a controller definition - see controllers.js for detals
 6    'duckDuckGo',
 7    [
 8        '$resource',
 9        function($resource) {
10            // we define a REST resource:
11            return $resource(
12                'http://api.duckduckgo.com/?format=json&q=:q', // this is a URL; please note the "q" parameter!
13                { callback: 'JSON_CALLBACK' }, // this is configuration only
14                { ask: { method: 'JSONP' } } // we define only one method here named "ask"
15                // there's way more to that - to understand in detail please check AngularJS docs.
16            );
17        }
18    ]
19);
20
21// we could use low-level $http in controller instead but using services has its advantages, primarily:
22// - they're separate - you can change them without any knowledge how controllers work
23// - they can provide a full set of REST methods - we're defining only one here but it is just a simple example.
24