Sha256: 1d76fb63cdcad2d0741e471a62eb3d7e443e9f84df39fa103b3c8b21ec479d3b

Contents?: true

Size: 1.17 KB

Versions: 32

Compression:

Stored size: 1.17 KB

Contents

!(angular => {
  'use strict';

  class ModelState {

    constructor(fields, comparators = {}) {
      this._fields = fields;
      this._comparators = this._defaultComparators(fields, comparators);
    }

    snapshotItem(item) {
      this._snapshot = angular.copy(item);
    }

    get snapshot() {
      return angular.copy(this._snapshot);
    }

    isPristine(item) {
      return _.every(this._fields, field => {
        return this._comparators[field] (
          this._resolve(field, item),
          this._resolve(field, this._snapshot)
        );
      });
    }

    isDirty(item) {
      return !this.isPristine(item);
    }

    // private methods

    _resolve(path, obj, safe = true) {
      return path.split('.').reduce((prev, curr) => {
        return !safe ? prev[curr] : (prev ? prev[curr] : undefined);
      }, obj || this);
    }

    _defaultComparators(fields, comparators) {
      let defaulted = {};
      _.each(fields, f => {
        defaulted[f] = comparators[f] || ((l, r) => JSON.stringify(l) === JSON.stringify(r));
      });
      return defaulted;
    }
  }

  angular.module('models.modelState', []).service('ModelState', () => ModelState);

}(angular));

Version data entries

32 entries across 16 versions & 1 rubygems

Version Path
aleph_analytics-0.4.9.pre.dev public/assets/angular/services/model/model_state.js-3e96e381ea7a9eff857081ffbf3a8bab.es6
aleph_analytics-0.4.9.pre.dev app/assets/javascripts/angular/services/model/model_state.js.es6
aleph_analytics-0.4.8 public/assets/angular/services/model/model_state.js-3e96e381ea7a9eff857081ffbf3a8bab.es6
aleph_analytics-0.4.8 app/assets/javascripts/angular/services/model/model_state.js.es6
aleph_analytics-0.4.7 app/assets/javascripts/angular/services/model/model_state.js.es6
aleph_analytics-0.4.7 public/assets/angular/services/model/model_state.js-3e96e381ea7a9eff857081ffbf3a8bab.es6
aleph_analytics-0.4.4 public/assets/angular/services/model/model_state.js-3e96e381ea7a9eff857081ffbf3a8bab.es6
aleph_analytics-0.4.4 app/assets/javascripts/angular/services/model/model_state.js.es6
aleph_analytics-0.4.2 public/assets/angular/services/model/model_state.js-3e96e381ea7a9eff857081ffbf3a8bab.es6
aleph_analytics-0.4.2 app/assets/javascripts/angular/services/model/model_state.js.es6
aleph_analytics-0.4.1 public/assets/angular/services/model/model_state.js-3e96e381ea7a9eff857081ffbf3a8bab.es6
aleph_analytics-0.4.1 app/assets/javascripts/angular/services/model/model_state.js.es6
aleph_analytics-0.3.0 public/assets/angular/services/model/model_state.js-3e96e381ea7a9eff857081ffbf3a8bab.es6
aleph_analytics-0.3.0 app/assets/javascripts/angular/services/model/model_state.js.es6
aleph_analytics-0.2.0 app/assets/javascripts/angular/services/model/model_state.js.es6
aleph_analytics-0.2.0 public/assets/angular/services/model/model_state.js-3e96e381ea7a9eff857081ffbf3a8bab.es6
aleph_analytics-0.1.0 public/assets/angular/services/model/model_state.js-3e96e381ea7a9eff857081ffbf3a8bab.es6
aleph_analytics-0.1.0 app/assets/javascripts/angular/services/model/model_state.js.es6
aleph_analytics-0.0.6 public/assets/angular/services/model/model_state.js-3e96e381ea7a9eff857081ffbf3a8bab.es6
aleph_analytics-0.0.6 app/assets/javascripts/angular/services/model/model_state.js.es6