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-osd-resource

v0.1.16

Published

[![Build Status](https://travis-ci.org/Osedea/angular-osd-resource.svg?branch=master)](https://travis-ci.org/Osedea/angular-osd-resource)

Downloads

3

Readme

Build Status

angular-osd-resource

This module provides an easy way to create and decorate angular $resource services from a config file. It also provides provides caching and pagination state decorators. This module was created with the following goals: clean, consistent API resources and easy to use caching and pagination.

Version

0.1.16

Installation and Setup

This package can be installed using bower:

$ bower install angular-osd-resource

Add the module to your app's module list:

angular.module(
            'app',
            [
                ...,
                'osdResource',
            ]
    )

Include a script tag (or add it to whatever you use to compile your js):

<script src="path/to/bower_components/angular-osd-resource/angular-osd-resource.min.js"></script>

Configuring Resources

All of your resources can be generated from a single configuration file. This is done through the ResourceConfigProvider. Here's an example configuration:

(function() {

    // @ngInject
    function resourceConfig(ResourceConfigProvider) {
        ResourceConfigProvider
            .global({
                decorators: ['cache', 'paginate'],
                methods: {
                    persist: { method: 'POST', isArray: false },
                }
            })
            .add('Document', '/api/documents/:id', {
                decorators: [ ... ],
                methods: { ... },
                relations: [ ... ],
            })
            .add('Form', '/api/forms/:id')
            .add('User', '/api/users/:id');
    }

    app.config(resourceConfig);
})();

In the example above we're defining three resources, Document, Form and User. Each of these resources will make API calls to their specified route.

ResourceConfigProvider also allows us to set global configuration on all of our resources. In this example, we're saying that every resource should use the cache and paginate decorator and every resource should have a persist method.

Although the config does allow us to create custom methods, keep in mind that we want our APIs to be as simple as possible. In most cases, it is better to use query params rather than create custom routes.

Generated Resources

Generated resources have the following API:

return {
    save: function (data, success, error) {
        return resource.save(data).$promise;
    },
    update: function (data, success, error) {
        return resource.update(data).$promise;
    },
    get: function (params, success, error) {
        return resource.get(params).$promise;
    },
    query: function (params, success, error) {
        return resource.query(params).$promise;
    },
    delete: function (id, success, error) {
        return resource.delete({id: id}).$promise;
    }
};

Note: Each resource method returns a $promise, therefore it needs to be handled using Angular's $promise API.

Note: If extra methods are provided, they can be called in the same way as the default methods.

Note: success and error are used to access underlying $http success and error callbacks on response. If you need to access response headers, this is the way to go.

How To Use Them

Here's an example of how to use a generated resource in a controller:

// @ngInject
myApp.controller('FormCtrl', function(Form) {

    // Form is the generated resource
    Form.query()
        .then(function(response) {
            var forms = response;
            // ...
        });
})

Resource Relations

When configuring resources, we can also specify relationships. For example, if a User has many Comments, we would like to be able to query /api/v1/users/:id/comments. To do this, we can specify an array of relations in our resource config file. See above for an example. To query the comments associated with the user of id = 1 , we can do the following:

// @ngInject
myApp.controller('UserCtrl', function(User) {

    // User has a Comments relation
    User.comments({ id: 1 })
        .then(function(response) {
            var comments = response;
            // ...
        });
})

Decorators

When generating resources, we can decorate them with common functions. For example, if we want to cache data being queried from the resource, we can use the cacheDecorator. See above for an example of how to include decorators on a resource.

The following decorators are available:

  • cache
  • paginate

Cache Decorator

The cache decorator will automatically cache data being queried from your API. The cache will also clear itself if an item is saved or updated, ensuring that our data will not become outdated. There are extra functions provided with this decorator:

// The next call using the resource resource will not use the cache. Following calls will use the cache.
CachedResource.setForced();

// Clear the entire cache for this resource.
CachedResource.clearCache();

Events

The cache decorator provides a single event listener for clearing all caches:

$rootScope.$on('osdResource.cache.clearAll');

Paginate Decorator

In our configuration file, we can specify that we want our resource to be decorated with a paginator. The paginate decorator manages the pagination state so we don't have to. The following methods are available for paginated resources:

PaginatedResource.query = function(params);

PaginatedResource.prevPage = function(params);

PaginatedResource.nextPage = function(params);

PaginatedResource.perPage = function(value);

PaginatedResource.page = function(value);

PaginatedResource.getTotalPage = function();

PaginatedResource.getTotalCount = function();

PaginatedResource.hasMore = function();

License

The MIT License (MIT)

Copyright (c) 2015 damacisaac

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.