Skip to content

dsilva2401/resources

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Resources module

Install

bower install resources

or download link

Import script

<script src="/bower_components/resources/dist/resources.min.js"></script>

Import module

var app = angular.module('myApp', ['resources']);

Usage

Init config

app.config( ['$resourcesProvider', function ($resourcesProvider) {
    $resourcesProvider.init({
        resources: {

            Entities: {
                route: '/api/entity/:entityId'
            },

            Workers: {
                route: '/api/entity/:entityId/worker/:workerId'
            }

        }
    });
}]);

Call resource

app.controller('myController', ['$scope', '$resources', function ($scope, $resources) {
  
    $resources.Entities.get().then(function (resp) {
        console.log(resp) // Return all entities from /api/entity/
    });
  
}]);

Api

$resourcesProvider.init(config)

config attributes

Attribute Type
httpConfig Object
domain String
resources object
  • httpConfig Sets http request configuration

Example

$resourcesProvider.init({
    httpConfig: {
        headers: {
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
            'Access-Control-Allow-Headers': 'Content-Type, X-Requested-With'
        }
    },
    // More options..
});
  • domain Set domain for requests

Example

$resourcesProvider.init({
    domain: 'http://123.45.6.78',
    // More options..
});
  • resources Set resources

Example

$resourcesProvider.init({
    resources: {

        Entities: {
            route: '/api/entity/:entityId'
        },

        Workers: {
            route: '/api/entity/:entityId/worker/:workerId'
        }

    }
    // More options..
});

$resources

Provide all resources set before, in this case $resources.Entities and $resources.Workers, each resources have methods GET, POST, PUT and DELETE.

Example

// Get and display all entities
// GET => /api/entity
$resources.Entity.get().then(function (entities) {
    console.log(entities);
}).catch(function (error) {
    console.error(error);
});

Each method can receive and object with two attributes: urlParams and data.

urlParams

Replace parameters passed in resource route

Example

// Get and display all workers from entity with id 123
// GET => /api/entity/123/worker
$resources.Workers.get({
    urlParams: { entityId: 123 }
}).then(function (workers) {
    console.log(workers);
}).catch(function (error) {
    console.error(error);
});
data

Sends data to resource

Example

// Create a worker in entity with id 123
// POST => /api/entity/123/worker
$resources.Workers.post({
    urlParams: { entityId: 123 },
    data: { name: 'John Smith', email: '[email protected]' }
}).then(function (worker) {
    console.log(worker); // Display created worker
}).catch(function (error) {
    console.error(error);
});

About

A REST Services interface for AngularJS

Resources

Stars

Watchers

Forks

Packages

No packages published