Module 4: Creating the Session Service

In the sidemenu starter app, the playlists are hardcoded in controllers.js. In this module, you create a Session service that uses the Angular resource module (ngResource) to retrieve the conference sessions using REST services.

Steps

  1. In the conference/www/js directory, create a file named services.js

  2. In services.js, define a module named starter.services with a dependency on ngResource:

    angular.module('starter.services', ['ngResource'])
    
  3. In that module, define a service named Session that uses the Angular resource module to provide access to the REST services at the specified endpoint:

    angular.module('starter.services', ['ngResource'])
    
    .factory('Session', function ($resource) {
        return $resource('http://localhost:5000/sessions/:sessionId');
    });
    

    In a real-life application, you would typically externalize the server parameters in a config module.

  4. The starter.services module you just created has a dependency on the Angular resource module which is not included by default. Open index.html and add a script tag to include angular-resource.min.js (right after ionic-bundle.js):

    <script src="lib/ionic/js/angular/angular-resource.min.js"></script>
    
  5. Add a script tag to include the services.js file you just created (right after app.js):

    <script src="js/services.js"></script>
    

comments powered by Disqus