npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

angular-datalayer

v2.0.4

Published

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.

Downloads

46

Readme

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
  .module('app', ['angular-datalayer'])
  .controller('Controller', function (datalayer) {
    var User = datalayer({ model: 'users' });
  });

or

var dataLayer = require('angular-datalayer');

angular
  .module('app', [
    dataLayer
  ])
  .controller('Controller', function (datalayer) {

  });

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: null
    • Required: true
  • version: The version of the API
    • Default: v1
    • Required: false
  • id_reference: Which field represents the unique identifier
    • Default: id
    • Required: false

Returns <class> Resource

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 = datalayer({ model: 'users' });

var john = new User();
var carlos = User.get({ id: 10 });

john.name = 'John';
john.surname = 'Howard';
john.age = '28';

carlos.age = 29;

john.$save()
.then(function (data) {
  console.log(data);
});
/**
 * Will print
 * {
 * 	id: 1,
 * 	name: 'John',
 * 	surname: 'Howard',
 * 	age: 28
 * }
 */

carlos
  .$save()
  .then(function (data) {
    ...
  });

User.get({ id: [10, 11, 12] })
.then(function (users){
 for (var i = 0, len = users.length; i < len; i++) {
  if (users[i].gender === 'male') {
   users[i].status = 'inactive';
  }

  users[i].$save();   // update
 }
});

User.query({ gender: { $eq: 'male' } })
  .then(function (users) {
    for (var i = 0, len = users.length; i < len; i++) {
      if (users[i].age > 70) {
        users[i].status = 'inactive';
      }

      users[i].$save();   // update
    }
  })
  .catch(function (error) {
    console.log(error);
  });

User.delete({ id: 20 });  // 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.moduel('app')
.controller('Controller1', function (scope, datalayer){
 var Task = datalayer({ model: 'task' });
 var subscriptionId = Task.$on('dl-task.save', onSave);

 function onSave(task) {
  alert('Task ' + task.name + ' created')!
 }

 scope.$on('$destroy', function() {
    Task.$off(subscriptionId);
 });
});

angular.moduel('app')
.controller('Controller2', function (scope, datalayer){
 var ref = datalayer({ model: 'task' });
 var subscriptionId = ref.$on('dl-save', onSave);

 function onSave(data) {
  alert('Something has been saved', data);
 }

 scope.$on('$destroy', function() {
    ref.$off(subscriptionId);
 });
});

Handling Pagination

lets say the api return a json not a array:

option 1
Preserving the total

{
 total: 150,
 items: [{...}, {...}]
}
var User = datalayer({ model: 'users' });

User.query({ gender: { $eq: 'male' }, page: 1, limit: 10 }, false)
.then(function (users){
 for (var i = 0, len = users.items.length; i < len; i++) {
  users.items[i] = new User(users.items[i])
 }
});

option 2 Using $http transformResponse option

var User = datalayer({ model: 'users' });

User.query({ gender: { $eq: 'male' }, page: 1, limit: 10 }, false, {
 transformResponse: function (data){
  if (data && data.items) {
   return data.items;
  }
 }
})
.then(function (users){

});