Services - jasperjs/jasper-application GitHub Wiki

Service - is's BLL (business logic layer) of your application. Unlike components and decorators, services does not contains any presentation logic.

To create service type:

yo jasper:service <AREANAME> <SERVICENAME>

For instance:

yo jasper:service profiles UsersService

New service will created in area folder. In the project service represented as class. At application lifetime, services instantiates as singletons (works over AngularJS services).

Example of the service:

module spa.profiles.services {
    // service can be injected as "MembershipService"
    export class MembershipService {
        // inject service here
        static $inject = [];

        constructor() {
        }

        validateUser(username: string, password: string): boolean {
            // place validation logic here
            return true;
        }
    }
}

This service has one public method, that validates user's infomation. When service created, it can be injected to any other part of application: component, decorator, another service etc...

module spa.profiles.components {
    // component renders user login form
    export class UserLoginForm {
        // inject service here
        static $inject = ['MembershipService'];

        // properties binds to inputs
        username: string; 
        password: string;

        constructor(private membershipService: services.MembershipService) {
        }

        submit() {
            if(this.membershipService.validateUser(this.username, this.password)) {
                // do something
            }
        }
    }
}

user-login-form.html:

<form ng-submit="vm.submit()">

  <p>Username:</p>
  <input type="text" ng-model="vm.username" />

  <p>Password:</p>
  <input type="text" ng-model="vm.password" />

  <button type="submit">Submit</button>

</form>

Good practice, that every service expose an interface. In this case you can mock an service implementation and use it on the test phase. The example above may be written like:

module spa.profiles.services {
    // interface declare a public contract of the service
    export interface IMembershipService {
        validateUser(username: string, password: string): boolean;
    }
    // service can be injected as "MembershipService"
    export class MembershipService implements IMembershipService  {
        // inject service here
        static $inject = [];

        constructor() {
        }

        validateUser(username: string, password: string): boolean {
            // place validation logic here
            return true;
        }
    }
}

After that you can use interface over implementation in your component:

module spa.profiles.components {
    export class UserLoginForm {
        // inject service here
        static $inject = ['MembershipService'];

        // properties

        constructor(private membershipService: services.IMembershipService) {
        }

        // methods
    }
}

With this approach, you can easily test any part of the application.

⚠️ **GitHub.com Fallback** ⚠️