Sha256: ac75d169da242c2fa19f17de021a2bd8416bb9b7787835cba5e91b452fefc1e1

Contents?: true

Size: 1.75 KB

Versions: 32

Compression:

Stored size: 1.75 KB

Contents

!(angular => {
  'use strict';

  function CollectionModelBaseImports($q) {

    return class CollectionModelBase {

      constructor(resource, Model) {
        this.collection = [];
        this._resource = resource;
        this._Model = Model;
      }

      get Model() {
        return this._Model;
      }

      internalize(items) {
        if(_.exists(items) && _.isArray(items) && items.length > 0) {
          this.collection = _.map(items, this._toModel.bind(this));
        } else {
          this.collection = [];
        }
      }

      initCollection(params) {
        return this._resource.index(params).$promise.then(items => {
          this.internalize(items);
          return this.collection;
        });
      }

      save(saveParams, initParams) {
        return this._saveCollection(saveParams, initParams, this.collection);
      }

      add(item) {
        let model = this._toModel(item);
        this.collection.unshift(model);
        return model;
      }

      remove(item) {
        let index = _.map(this.collection, m => m.item.id).indexOf(item.id);
        return this.collection.splice(index, 1)[0];
      }

      items() {
        return _.map(this.collection, o => o.item);
      }

      // protected methods

      _saveCollection(saveParams, initParams, collection) {
        return $q.all(
            _.map(collection, m => m.save(saveParams))
          ).then(this.initCollection.bind(this, initParams));
      }

      // private methods

      _toModel(item) {
        let m = new this._Model();
        m.internalize(item);
        return m;
      }
    };
  }

  CollectionModelBaseImports.$inject = ['$q'];

  angular.module('models.collectionModelBase', [])
    .service('CollectionModelBase', CollectionModelBaseImports);

}(angular));

Version data entries

32 entries across 16 versions & 1 rubygems

Version Path
aleph_analytics-0.0.5 app/assets/javascripts/angular/services/model/collection_model_base.js.es6
aleph_analytics-0.0.5 public/assets/angular/services/model/collection_model_base.js-8092335acde425d28718c1a9fc677d39.es6
aleph_analytics-0.0.4 app/assets/javascripts/angular/services/model/collection_model_base.js.es6
aleph_analytics-0.0.4 public/assets/angular/services/model/collection_model_base.js-8092335acde425d28718c1a9fc677d39.es6
aleph_analytics-0.0.3 public/assets/angular/services/model/collection_model_base.js-8092335acde425d28718c1a9fc677d39.es6
aleph_analytics-0.0.3 app/assets/javascripts/angular/services/model/collection_model_base.js.es6
aleph_analytics-0.0.2 public/assets/angular/services/model/collection_model_base.js-8092335acde425d28718c1a9fc677d39.es6
aleph_analytics-0.0.2 app/assets/javascripts/angular/services/model/collection_model_base.js.es6
aleph_analytics-0.0.1.alpha app/assets/javascripts/angular/services/model/collection_model_base.js.es6
aleph_analytics-0.0.1.alpha public/assets/angular/services/model/collection_model_base.js-8092335acde425d28718c1a9fc677d39.es6
aleph_analytics-0.0.0.alpha public/assets/angular/services/model/collection_model_base.js-8092335acde425d28718c1a9fc677d39.es6
aleph_analytics-0.0.0.alpha app/assets/javascripts/angular/services/model/collection_model_base.js.es6