0.1.0 • Published 8 years ago

angular2-trails-realtime v0.1.0

Weekly downloads
5
License
MIT
Repository
github
Last release
8 years ago

angular2-trails-realtime An angular module for using the trails-realtime with primus in angular2 (@angular)

INDEX

Usage with angular-cli

Angular-cli is a great angular2 app starter for creating fancy angular stuff.

Here I will describe on how to install and use it:

1. Installing

install the package with the node package manager (npm)

npm install --save angular2-trails-realtime

back to top

2. Configure angular-cli

back to top

APP_ROOT/angular-cli-build.js

Here you may add a line of code, to add the package to your vendor files

var Angular2App = require('angular-cli/lib/broccoli/angular2-app');

module.exports = function (defaults) {
  return new Angular2App(defaults, {
    vendorNpmFiles: [
     ...
      'angular2-trails-realtime/dist/**/*.+(js|js.map)',
    ]
  })
    ;
};

back to top

APP_ROOT/src/system-config.ts

Here you have to register the module in the systemJS configuration and define the mapping location and the entry point.

/** Map relative paths to URLs. */
const map:any = {
  ...
  'angular2-trails-realtime': 'vendor/angular2-trails-realtime/dist'
};
/** User packages configuration. */
const packages:any = {
    ...,
    'angular2-trails-realtime': {
        defaultExtension: 'js',
        main: 'index.js'
      },
}

back to top

3. Using it

back to top

the import statement looks like this:

import { TrailsService } form 'angular2-trails-realtime';

back to top

You can provide the TrailsService in a Component by the provider array or (not recommended) by providing it in the bootstrap of your application.

Example in Component:

import { Component, OnInit } from '@angular/core';
import { TrailsService } from 'angular2-trails-realtime';

@Component({
    moduleId: module.id,
    selector: 'my',
    templateUrl: 'my.component.html',
    providers: [TrailsService]
})
export class MyComponent implements OnInit {
    constructor() { }

    ngOnInit() { }

}

// and in a subcomponent of the provider component

import { Component, OnInit } from '@angular/core';
import { TrailsService } from 'angular2-trails-realtime';

@Component({
    moduleId: module.id,
    selector: 'my-sub',
    templateUrl: 'my-sub.component.html',
})
export class MySubComponent implements OnInit {
    constructor(private _TrailsService:TrailsService) { }

    ngOnInit() { }

}

Example in bootstrap (not recommended):

...
import { TrailsService } from 'angular2-trails-realtime';

bootstrap(AppComponent, [
  ...,
  TrailsService
]);


// and then again in a component

import { Component, OnInit } from '@angular/core';
import { TrailsService } from 'angular2-trails-realtime';

@Component({
    moduleId: module.id,
    selector: 'app',
    templateUrl: 'app.component.html',
})
export class AppComponent implements OnInit {
    constructor(private _TrailsService:TrailsService) { }

    ngOnInit() { }

}

back to top

working with it

You inject the service by the constructor where you want to use it.

constructor(private _TrailsService:TrailsService) { }

first you have to connect your service:

ngOnInit() {
    this._TrailsService.connect()
}

you can pass in an Url or Options, where to connect to

ngOnInit() {
    this._TrailsService.connect("http://localhost:1337");
    // or
    letl opts = {
        url: "http://localhost:1337",
        transports: ['polling', 'websocket'],
        headers: {...},
        ...
    }
    this._TrailsService.connect(opts);
}

for more information, please visit sailsjs.org Documentation for SailsSocket Properties

this is handy, when you develop with angular-cli (localhost:4200) and the ng serve command and your sails app runs separately e.g on localhost:1337

The following methods are implemented in the TrailsService and will always return you an Observable:

  • get(path,data):Observable
  • post(path,data):Observable
  • put(path,data):Observable
  • delete(path,data):Observable
  • request(options):Observable
  • on(eventEntity):Observable

for more information, please visit sailsjs.org Documentation for SailsSocket Methods

You then have to subscribe to that Observable, to get the data.

back to top

EXAMPLE:

<ul>
    <li *ngFor="let user of users">{{user.firstname}} {{user.lastname}}</li>
</ul>
import { Component, OnInit } from '@angular/core';
import { TrailsService } from 'angular2-trails-realtime';

@Component({
    moduleId: module.id,
    selector: 'app',
    templateUrl: 'app.component.html',
})
export class AppComponent implements OnInit {

    // declare the variable for the template
    public users:any[];

    constructor(private _TrailsService:TrailsService) { }

    ngOnInit() {

    this._TrailsService
         .get('/users')
         .subscribe(
            (resData) => { this.users = resData.data},
            (error) => { console.log("oooops, error occured") }
            () => { console.log("we are finished") }
        )

    }
    
    /*
     * explanation of observable response object
     * 
     * resData = {
     *      data: <object>,
     *      statusCode: <number>,
     *      response: <jwres>,
     *      error: <undefined>
     * }
     * 
     * error = {
     *      data: null,
     *      statusCode: <number>,
     *      response: <jwres>,
     *      error: <jwres.error>
     * }
     */

}

back to top

EXAMPLE with Async Pipe

or even better, you use the async pipe of angular, and just pass the Observable to it

<ul>
    <li *ngFor="let user of users$ | async">{{user.firstname}} {{user.lastname}}</li>
</ul>
import { Component, OnInit } from '@angular/core';
import { TrailsService } from 'angular2-trails-realtime';
import { Observable } from "rxjs/Rx";

@Component({
    moduleId: module.id,
    selector: 'app',
    templateUrl: 'app.component.html',
})
export class AppComponent implements OnInit {

    // declare the variable for the template
    public users$:Observable<any[]>;

    constructor(private _TrailsService:TrailsService) { }

    ngOnInit() {

    // now we are passing the Observable to the template variable
    this.users$ = this._TrailsService.get('/users');

    }

}

back to top