all files / documents/document/internal/ array.js

100% Statements 36/36
100% Branches 4/4
100% Functions 13/13
100% Lines 31/31
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94                          22× 22×       18×       19×             18× 18×             11×       11× 11× 11×                       15×       24×   24× 24×   24× 28× 28×     24×       29×       11×          
import Ember from 'ember';
import InternalBase, { empty } from './base';
import { toModel, toInternal, isInternal } from 'documents/util/internal';
 
const {
  A
} = Ember;
 
export default class InternalArray extends InternalBase {
 
  static get type() {
    return 'array';
  }
 
  constructor(store, parent) {
    super(store, parent);
    this.values = A();
  }
 
  _createModel() {
    return this.store._createArrayModel(this);
  }
 
  get _valueObserverOptions() {
    return {
      willChange: this._valueWillChange,
      didChange: this._valueDidChange
    };
  }
 
  _didCreateModel() {
    super._didCreateModel();
    this.values.addEnumerableObserver(this, this._valueObserverOptions);
  }
 
  _didDestroyModel() {
    super._didDestroyModel();
    this.values.removeEnumerableObserver(this, this._valueObserverOptions);
  }
 
  _valueWillChange(array, removing) {
    A(removing).forEach(internal => this._detachInternal(internal));
  }
 
  _valueDidChange(array, removeCount, adding) {
    A(adding).forEach(internal => this._attachInternal(internal));
    this._didEndPropertyChanges();
    this._dirty();
  }
 
  toInternal(value, type='model') {
    value = toInternal(value);
 
    if(isInternal(value)) {
      if(value.isDetached()) {
        return value;
      } else {
        value = value.serialize(type);
      }
    }
 
    let { internal } = this._deserializeValue(value, empty, type);
    return internal;
  }
 
  toModel(internal) {
    return toModel(internal);
  }
 
  _deserialize(values, type) {
    let current = this.values;
 
    current.forEach(internal => this._detachInternal(internal));
    current.clear();
 
    let internals = A(values).map(value => {
      let { internal } = this._deserializeValue(value, empty, type);
      return internal;
    });
 
    current.addObjects(internals);
  }
 
  _serialize(type) {
    return this.values.map(value => this._serializeValue(value, type));
  }
 
  _dirty() {
    this.withPropertyChanges(changed => super._dirty(changed), true);
  }
 
}