1.0.0-beta.14 • Published 6 years ago

collection-json-angular v1.0.0-beta.14

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

Known Vulnerabilities

Collection+JSON Angular Implementation

Disclaimer

This project is under development (beta state), use it with care. API changes might occur.

The library is not yet a "true" angular library, refer to the Usage

Usage

The library needs to be configured with the proper HttpClient service to work For this create a module like this and import it in your root module:

import {NgModule} from '@angular/core';
import {HttpClient, HttpClientModule} from '@angular/common/http';
import {CollectionConfigurationManager} from 'collection-json-base/models'

@NgModule({
  declarations: [],
  exports: [],
  imports: [HttpClientModule],
  providers: [],
})
export class AngularCollectionModule {
  // Set up collection-json-base library with the proper http service
  constructor(private httpClient: HttpClient) {
    try {
      CollectionConfigurationManager.getHttpService<HttpClient>();
    } catch (e) {
      CollectionConfigurationManager.setHttpService<HttpClient>(this.httpClient);
    }
  }
}

To request a Collection+JSON type information using the HttpClient the WrappedCollectionJSON interface from the base library can be used. Every collection will come in a form { collection: CollectionJSON }. The AngularCollection constructor can accept either a WrappedCollectionJSON or a CollectionJSON:

import {WrappedCollectionJSON} from 'collection-json-base';

 this.httpClient.get<WrappedCollectionJSON>('http://example.com').pipe(map(
    (wrappedCollectionJson) => new AngularCollection(wrappedCollectionJson)
))

Follow a link (request the collection it points to), the link of a collection or item can be accessed using its rel property:

const collection = new AngularCollection(someCollection);
const optionalParameters = new HttpParams();

collection.link('link_rel').follow(optionalParamateres).subscribe(
    (requestedCollection: AngularCollection) => {
        // You can use the received requestedCollection which is already an AngularCollection type.
    }
);

Get the items of a collection:

const collection = new AngularCollection(someCollection);
let item: AngularItem;
let itemStore: AngularItemStore;

try {
    itemStore = collection.items(); // Throws an error if there are no items on the collection
} catch (e) {
    console.error(e.message);
}

// Let's assume that the collection has items from now on

try {
    item = collection.items().one(); // Returns only one item, throws an error if there are more than one
} catch (e) {
    console.error(e.message);
}

item = collection.items().first(); // Returns the first item or undefined

// Returns all of the items on the collection in an AngularItem array.
let items: AngularItem[] = collection.items().all(); 

Prepare and send a template:

const collection = new AngularCollection(someCollection);
const template = collection.template();

// Setting a property
template.set("propertyName", "propertyValue");

// Setting multiple properties using a JavaScript object
template.setWithDataObject(
    {
        "propertyName1" : "propertyValue1",
        "propertyName2" : "propertyValue2",
    }
);

// Send a POST request
template.submit().subscribe(
    (result: AngularCollection) => {
        // This might be empty, depends on the backend
    },
    (error) => {
        if (error instanceof Error) {
           // Validation error
        } else {
           // Server error
        }
    }
);

// Send a PUT request
template.update().subscribe(
    (result: AngularCollection) => {
        // This might be empty, depends on the backend
    },
    (error) => {
        if (error instanceof Error) {
           // Validation error
        } else {
           // Server error
        }
    }
);
1.0.0-beta.14

6 years ago

1.0.0-beta.13

6 years ago

1.0.0-beta.12

6 years ago

1.0.0-beta.11

6 years ago

1.0.0-beta.10

6 years ago

1.0.0-beta.9

7 years ago

1.0.0-beta.8

7 years ago

1.0.0-beta.7

7 years ago

1.0.0-beta.6.5

7 years ago

1.0.0-beta.6.4

7 years ago

1.0.0-beta.6.3

7 years ago

1.0.0-beta.6.2

7 years ago

1.0.0-beta.6.1

7 years ago

1.0.0-beta.6

7 years ago

1.0.0-beta.5

7 years ago

1.0.0-beta.4

7 years ago

1.0.0-beta.3

7 years ago

1.0.0-beta.2

7 years ago

1.0.0-beta.1

7 years ago

1.0.0-beta.0

7 years ago

1.0.0-alpha.47

7 years ago

1.0.0-alpha.46

7 years ago

1.0.0-alpha.45

7 years ago

1.0.0-alpha.44

7 years ago

1.0.0-alpha.43

7 years ago

1.0.0-alpha.42

7 years ago

1.0.0-alpha.41

7 years ago

1.0.0-alpha.40

7 years ago

1.0.0-alpha.39

7 years ago

1.0.0-alpha.38

7 years ago

1.0.0-alpha.37

7 years ago

1.0.0-alpha.36

7 years ago

1.0.0-alpha.35

7 years ago

1.0.0-alpha.34

7 years ago

1.0.0-alpha.33

7 years ago

1.0.0-alpha.32

7 years ago

1.0.0-alpha.31

7 years ago

1.0.0-alpha.30

7 years ago

1.0.0-alpha.29

7 years ago

1.0.0-alpha.28

7 years ago

1.0.0-alpha.27

7 years ago

1.0.0-alpha.26

7 years ago

1.0.0-alpha.25

7 years ago

1.0.0-alpha.24

7 years ago

1.0.0-alpha.23

7 years ago

1.0.0-alpha.22

7 years ago

1.0.0-alpha.21

7 years ago

1.0.0-alpha.20

7 years ago

1.0.0-alpha.19

7 years ago

1.0.0-alpha.18

7 years ago

1.0.0-alpha.17

7 years ago

1.0.0-alpha.16

7 years ago

1.0.0-alpha.15

7 years ago

1.0.0-alpha.14

7 years ago

1.0.0-alpha.13

7 years ago

1.0.0-alpha.12

7 years ago

1.0.0-alpha.11

7 years ago

1.0.0-alpha.10

7 years ago

1.0.0-alpha.9

7 years ago

1.0.0-alpha.8

7 years ago

1.0.0-alpha.7

7 years ago

1.0.0-alpha.6

7 years ago

1.0.0-alpha.5

7 years ago

1.0.0-alpha.4

7 years ago

1.0.0-alpha.3

7 years ago