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