Skip to content
mzilic edited this page Mar 31, 2015 · 12 revisions

Baasic Module Architecture

To get a better understanding of Baasic AngularJS services bellow are the details of the main architecture to which all the library services conform to.

SDK Documentation

Core Services

  • baasicApp service is used to manage Baasic application instances. Multiple AngularJS application instances can be created and coexist at the same time (each will communicate with its corresponding Baasic application)

    • create an application
    module.controller("MyCtrl", ["baasicApp",
        function MyCtrl(baasicApp) {
            var app = baasicApp.create("<api-key>", {
                apiRootUrl: "api.baasic.com",
                apiVersion: "<version>"
            });
        }]);
    • get the default application
    module.controller("MyCtrl", ["baasicApp",
        function MyCtrl(baasicApp) {
            var app = baasicApp.get();
        }]);
    • application object has the following methods
    var apiKey = app.get_apiKey();
    var apiURI = app.get_apiUrl();
    var accessToken = app.get_accessToken();
    app.update_accessToken(accessToken);
    var currentUser = app.get_user();
    app.set_user(userDetails, accessToken);
    var currentLanguage = app.get_currentLanguage();
    var defaultLanguage = app.get_defaultLanguage();
  • baasicApiHttp

    Baasic HTTP service is used to perform low level communication with the Baasic back-end.

    This service handles:

    • authentication tokens
    • HAL parsing
  • baasicApiService

    This service is used to perform low level model or option transformations before they are sent to the Baasic back-end.

    The following transformations are supported:

    • Resource collection fetch transformation
    • Single resource fetch transformation
    • Create resource transformation
    • Update resource transformation
    • Delete resource transformation
  • baasicConstants

Baasic constants contain values such as id property name and model property name parameters that can be used in case manual model or option transformation is needed.

Route Services

  • every service has a route service used to wrap REST service URL discovery
  • route service parses the REST service URL and prepares the URL for expansion
  • route services contain the following routes
    • find - used to fetch a collection of resources that can be filtered, paged, sorted
    • get - used to fetch a single resource
    • create - used to create a new resources
  • supported route parameters:
    • find route supports the following parameters:
      • searchQuery - used to build simple filters or complex queries (read more about Baasic Query Language in Baasic User Manual)
      • page - used to define the current page
      • rpp - used to define the number of resources per page
      • sort - used to define sorting expression applied to the returned resources. Sorting expressions use the following format: field1Name|asc,field2Name|desc
      • embed - used to embed additional resources
      • fields - used to define the list of fields returned by the service
    • get route supports the following parameters:
      • embed - used to embed additional resources
      • fields - used to define list of fields returned by the service
    • create route in most cases has no parameters
  • parse is an utility method used to parse custom URIs (Note: parse will not return a route)

Module Services

  • Baasic module services are built on top of AngularJS services
  • module services depend upon route services as they are used for REST service URL discovery (Note: every service exposes route service through the routeService property)
  • every service has the find, get, create, update and remove functions used to communicate with the Baasic back-end

HAL links

Resources returned from Baasic are by default in HAL format and they look similar to this:

{
    "key": "your-key",
    "value": "your-value",
    "dateCreated": "2015-01-01T14:24:56.795849Z",
    "dateUpdated": "2015-01-01T14:24:56.795849Z",
    "id": "4MKkskdd1213mmkk",
    "_links": {
        "self": {
            "href": "http://api.baasic.local/<version>/your-app-id/key-values/your-key",
            "templated": false
        },
        "post": {
            "href": "http://api.baasic.local/<version>/your-app-id/key-values",
            "templated": false
        },
        "put": {
            "href": "http://api.baasic.local/<version>/your-app-id/key-values/your-key",
            "templated": false
        },
        "delete": {
            "href": "http://api.baasic.local/<version>/your-app-id/key-values/your-key",
            "templated": false
        }
    },
    "_embedded": { }
}

Note: The preferred way of accessing Bassic back-end is through use of AngularJS SDK services. For manual access to the back-end URL's you can use this (or similar) code:

data.links('put').href
data.links('delete').href

You can for example also create your own update function by using Baasic core services:

update: function (data) {
    var params = baasicApiService.updateParams(data);
    var url = params[baasicConstants.modelPropertyName].links('put').href;
    var model = params[baasicConstants.modelPropertyName];
    return baasicApiHttp.put(url, model);
},

Extending models

Baasic built-in models can be extended with custom properties by simply setting property values.

article.myProperty = 1;
article.myPropertyObject = {
    firstProp: 1,
    secondProp: 2
}

update: function (article) {
    return articleService.update(article);
}

This powerful feature allows you to extend all built-in models with custom properties which allows modules like Article to fully suit your needs.