Skip to content

A collection management service leveraging lodash for AngularJS

License

Notifications You must be signed in to change notification settings

typehorror/angularjs-collection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

angularjs-collection

An Angular JS service to manage collections of objects with a primary key constraint.

Overview

Import the collection module as a dependency of your module.

angular.module('my_app', ['collection'])

Create a store leveraging the Collection class

angular.module('my_app').service 'dataStore',
  (
    'Collection'
  ) ->

    collection: new Collection({primary_key: 'id'})

Use the store in your controllers

angular.module('my_app').controller 'DataController',
  (
    'dataStore'
  ) ->

    # filter by primary key
    selected_ids = [12, 1, 44]
    selected_objects = dataStore.collection.where(selected_ids)

    # filter by attribute
    engineers = dataStore.collection.where(title: 'engineer')

    # delete by primary key
    dataStore.collection.remove([2,5,6])

    # delete by attribute
    dataStore.collection.remove(title: 'engineer')

    # Retrieve a copy of the collection
    model_copy = dataStore.collection.models

    model_copy == dataStore.collection.models           # false
    _.isEqual(model_copy, dataStore.collection.models)  # true

    # Add will overwrite any existing record matching the added
    # object primary key
    dataStore.collection.add [
      {id: 123, title: 'engineer'}
      {id: 124, title: 'architect'}
    ]

Proxy methods

Those methods are simple proxy to lodash and aren't documented here. You may find their documentation on lodash's website

Those proxy methods conveniently receive the collection's content as an argument:

dataStore.collection.pluck('first_name')
# >>> 'john', 'steve', 'anna'


_.pluck(dataStore.collection.models, 'first_name')
# >>> 'john', 'steve', 'anna'
# same but a bit less efficient because .models returns a clone of the collection

Proxy methods are:

About

A collection management service leveraging lodash for AngularJS

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published