Datalayer
Datalayer is a simple AngularJS service abstraction to consume data from a RESTful Api. The code base is very simple to change and adapt to your backend in case it is not RESTful.
Install
You can download it by:
- Using npm and running
npm install angular-datalayer --save
- Download manually and include the
<script type="text/javascript" src="./path/to/your/datalayer.min.js">
Starter guide
angular ;
or
var dataLayer = ; angular ;
Usage
datalayer({ url: '<string>', model: '<string>', version: '<sctring>', id_reference: '<string>' })
Configuration Json
You can in any point modify ajax calls by specifying the request attribute the desire configurations following the angularjs $http configuration json. Please check the documention before adding any change.
url: '.' model: '' version: 'v1' id_reference: 'id' request: query: method: 'GET' get: method: 'GET' all: method: 'GET' $save: method: 'POST' $update: method: 'PUT' delete: method: 'DELETE' ;
Arguments
- url: Base url point to API
Default
: ./Required
: false
- model: Represents the resource of the api
Default
: nullRequired
: true
- version: The version of the API
Default
: v1Required
: false
- id_reference: Which field represents the unique identifier
Default
: idRequired
: false
<class>
Resource
Returns Returns the class Resource
with the default actions attached
Resource.query(query, isArray = true, [config])
Params: <object>
query ex: { age: { $gt: 15 } }
Params: <boolean>
isArray [optional] default: true
Params: <object>
config [optional]
Returns: <array>
Resource
Resource.get(option, [config])
Params: <object>
option ex: { id: <number> }
Params: <object>
config [optional]
Returns: <object>
|<Array>
Resource
Resource.all([config])
Params: <object>
config [optional]
Returns: <array>
Resource
Resource.delete(option, [config])
Params: <object>
option ex: { id: <number> }
Params: <object>
config [optional]
Returns: null
Code examples
var User = ; var john = ;var carlos = User; johnname = 'John';johnsurname = 'Howard';johnage = '28'; carlosage = 29; john;/** * Will print * { * id: 1, * name: 'John', * surname: 'Howard', * age: 28 * } */ carlos ; User; User ; User; // delete
Using events
Datalayer by default dispatch two main events, dl-save and dl-[model].save, when a save or update operation occours. We can listen to a particular model events, lets say dl-task.save
or when a save operations happened dl-save
.
angular; angular;
Handling Pagination
lets say the api return a json not a array:
option 1
Preserving the total
total: 150 items: ... ...
var User = ; User;
option 2 Using $http transformResponse option
var User = ; User;