Coder Social home page Coder Social logo

angular-delegator's Introduction

Build Status Coverage Status Code Climate

angular-delegator

Write smaller, cleaner AngularJS services.

Break large services into smaller services with the same interface. Angular-delegator will wire them up for you based on your configuration.

Please note: Angular Delegator is a very young project. The API is likely to change in the near future.

Example

If you had a large validation service that checked an account's name, date, address and payment info, you could break it down like so:

angular.module('myApp', ['delegator'])

  .factory('AccountValidator', function(Delegator) {

    return Delegator.create({
      interface: {
        'isValid': 'all'
      },
      delegates: [
        'AccountNameValidator',
        'AccountDateValidator',
        'AccountAddressValidator',
        'AccountPaymentValidator'
      ]
    });

  });

The interface option defines which methods should be handled by the delegator, and which delegator strategy to use. If none of the built-in strategies are sufficient, you can easily write your own, easily testable custom delegator strategies.

In this example, your application now has an AccountValidator service with a single method called isValid:

angular.module('myApp').controller('SomeCtrl', function($scope, AccountValidator) {

  AccountValidator.isValid($scope.data);

});

Automatically Generated Delegator Services

Rather than manually creating a delegator service, it can be generated for you dynamically in your module's config step:

angular.module('myApp', ['delegator'])

  .config(function(DelegatorProvider) {

    DelegatorProvider.service('AccountValidationDelegator', {
      interface: {
        'isValid': 'all'
      },
      delegates: [
        'AccountNameValidationDelegate',
        'AccountDateValidationDelegate',
        'AccountAddressValidationDelegate',
        'AccountPaymentValidationDelegate'
      ]
    });

  });

Delegator Strategies

<tr>
  <th>map</th>
  <td>Returns an array of everything returned from the delegates</td>

<tr>
  <th>merge</th>
  <td>Returns a single object, the result of merging all objects returned from the delegates</td>

<tr>
  <th>truthy</th>
  <td>Returns an array of all truthy values returned from the delegates</td>

<tr>
  <th>any</th>
  <td>Returns true if any delegate returns true</td>

<tr>
  <th>all</th>
  <td>Returns true if all delegates return true</td>

<tr>
  <th>none</th>
  <td>Returns true if no delegates return true</td>

Custom Delegator Strategies

Strategies are implemented as services ending with 'DelegatorStrategy'. All built-in delegator strategies are defined this way, and can be injected into your custom strategies.

For example,

angular.module('myApp')

  .factory('FoobarDelegatorStrategy', function(AllDelegatorStrategy) {
    return function(fns, args) {
      // Here you have access to the delegate functions
      // and the arguments passed to the delegator.
      return AllDelegatorStrategy(fns, args);
    };
  })

  .config(function(DelegatorProvider) {
    DelegatorProvider.service('AccountValidationDelegator', {
      interface: {
        'isValid': 'foobar'
      },
      delegates: [
        'AccountNameValidationDelegate',
        'AccountDateValidationDelegate',
        'AccountAddressValidationDelegate',
        'AccountPaymentValidationDelegate'
      ]
    });

  });

Installation

Bower

$ bower install --save angular-delegator

Download

Download the production version or the development version.

License

MIT License

angular-delegator's People

Contributors

markdalgleish avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.