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-repository

v0.1.12

Published

API Repository factory for Angular Js based on ngResource

Downloads

24

Readme

angular-repository

API Repository factory for Angular Js based on ngResource

Installation

Install with bower:

$ bower install angular-vrepository --save

Install with npm:

$ npm install angular-repository

Load the angular-repository module in your app.

angular.module('app', ['vRepository']);

Configure

    angular
        .module('app', [
            'vRepository',
        ])
        .config(['RepositoryFactoryProvider', config])
    ;
        
    function config(RepositoryFactoryProvider) {
        var myConfig = {
            url: 'http://api.com'           //required config parameter
            onError: (response) => {        //optional global callback
                return response;
            },
            onSuccess: (response) => {      //optional global callback example
                if (this.checkPropertyExistence(response, ['data'])) {
                    let data = response.data;
                    if (data instanceof Array) {
                        for (var key in data) {
                            if (data.hasOwnProperty(key)) {
                                data[key] = new this.model(data[key]);
                            }
                        }
    
                        return data;
                    } else {
                        return new this.model(data);
                    }
                }
            }
        };
        RepositoryFactoryProvider.configure(myConfig);
    }

    /**
     * Check if property exist
     *
     * @param obj
     * @param paths
     * @returns {boolean}
     */
    function checkPropertyExistence(obj, paths = []) {
        for (var i = 0; i < paths.length; i++) {
            if (!obj || !obj.hasOwnProperty(paths[i])) {
                return false;
            }
            obj = obj[paths[i]];
        }
        return true;
    }

Usage Example

Example usage:

Create your entity class

                        
//remeber that your mdoel class has to extend Entity class provider by this package
class User extends Entity {
    constructor(parameters) {
        //this 2 lines are required !!!
        let entity = super(parameters);
        if (entity.id) return entity;
        
        this.id = parameters.id;
        this.email = parameters.email;
        this.name = parameters.name;
        
        //after parameter remember to turn on watcher
        //so multiple API call will not reset your changes
        this.watch();
    }
}

Example List controller for your model

export class ListController {
    static $inject = ['$scope', 'RepositoryFactory'];
    
    this.repository = this.getRepository(User, '/users');

    constructor($scope, factory) {
        this.factory = factory;
        this.$scope = $scope;

        this.$scope.$watch(() => {
            return this.page
        }, this.onChange.bind(this));

        this.$scope.$watch(() => {
            return this.limit
        }, this.onChange.bind(this));
    }

    onChange(newValue, oldValue) {
        if (newValue !== oldValue) {
            this.repository.getAll({
                page: this.page,
                limit: this.limit
            }).then((response) => {
                this.items = response;
            });
        }
    }

    getRepository(model, path) {
        return this.factory.getRepository(model, path);
    }
}

You can also provide onSuccess and onError callback to the getRepository method

export class ListController {

    //....

    getRepository(model, path) {
        return this.factory.getRepository(model, path, this.onSuccess, this.onError);
    }
    
    onError(response) {
        return response;
    }
    
    onSuccess(response) {
        return response;
    }
}

They will override global callback provided in your config for this specific model only.