Skip to content
This repository has been archived by the owner. It is now read-only.
Go to file

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

Data Connector


Kind of a little client side DB, mostly an ajax abstraction layer.

Intended Use

Include dataConnector in your app initialization.

Make collections with the collection factory.

  .factory('Cat', ['collection', function Cat(store) {
    return collection.create('cats');

  .factory('Dog', ['collection', function Dog(store) {
    return collection.create('dogs');

The store is a POJO with a key for each collection.

{ cats: [], dogs: [] }

Collections are decorated instances of Array. They look and behave like arrays, they just have some extra juice.

var felix = Cat.add({ id: 1, name: 'Felix' });

Cat.forEach(function (cat) {
// logs { id: 1, name: 'Felix' }

// returns [{ id: 1, name: 'Felix' }]

// returns { id: 1, name: 'Felix' }

Before you use your collection in a controller you should load your data in a resolve function. Collections have #fetch and fetchAll methods for making get requests to your API.

$stateProvider.state('app.cats', {
  url: '/cats',
  title: 'Cats',
  resolve: {
    load: function(Cat) {
      return Cat.fetchAll();

$stateProvider.state('', {
  url: '/:catId',
  title: 'Cat',
  resolve: {
    load: function($stateParams, Cat) {
      return Cat.fetch($;

When the promise returned by the #fetchAll method resolves the store will be full of cats...

Now use your collection service whenever you need something from the collection.

  .controller('CatIndexCtrl', ['Cat', function ($scope, Cat) {
    $scope.cats = Cat.all();

Because Angular has instanitated both our store and Cat services only once, the same collection is available throughout your app.

From the docs

"Note: All services in Angular are singletons. That means that the injector uses each recipe at most once to create the object. The injector then caches the reference for all future needs."

  .controller('DogIndexCtrl', function ($scope, Dog, Cat) {
    $scope.dogs = Dog.all();
    $scope.cats = Cat.all(); // This is exactly the same collection as above

Model Relationships

When you define a collection you get the chance to provide some relationship data.

// The Cat service
return collection.create('cats', { 
  relationships: {
    'dogs': {
      kind: 'hasMany',
      collection: 'dogs',
      foreignKey: 'cat_id'

// The Dog service
return collection.create('dogs');

// ... after data has been recieved from an API records are added to the store.
Cat.add({ id: 1, name: 'Felix' });
Dog.add({ id: 1, name: 'Fido', cat_id: 1 });

// Some controller
// returns [{ id: 1, name: 'Fido', cat_id: 1 }]

Arrays returned from a relationship are similarly decorated with helper functions. They also contain a reference to their master collection in the sore.


Our Angular persistence library



No releases published


No packages published
You can’t perform that action at this time.