Exemple #1
0
    async ['@test observers that contain @each in the path should fire only once the first time they are accessed'](
      assert
    ) {
      let count = 0;

      let obj = EmberObject.extend({
        init() {
          this._super(...arguments);
          // Observer does not fire on init
          set(this, 'resources', emberA());
        },

        commonDidChange: emberObserver('*****@*****.**', () => count++),
      }).create();

      // Observer fires first time when new object is added
      get(obj, 'resources').pushObject(EmberObject.create({ common: 'HI!' }));
      await runLoopSettled();

      // Observer fires second time when property on an object is changed
      set(objectAt(get(obj, 'resources'), 0), 'common', 'BYE!');
      await runLoopSettled();

      assert.equal(count, 2, 'observers should be called twice');
    }
Exemple #2
0
function findIndex(array, predicate, startAt) {
  let len = array.length;
  for (let index = startAt; index < len; index++) {
    let item = objectAt(array, index);
    if (predicate(item, index, array)) {
      return index;
    }
  }
  return -1;
}
Exemple #3
0
    let contentDidChange = (array, idx, removedCount, addedCount) => {
      for (let i = idx; i < idx + addedCount; i++) {
        let record = objectAt(array, i);
        let wrapped = this.wrapRecord(record);
        releaseMethods.push(this.observeRecord(record, recordUpdated));
        recordsAdded([wrapped]);
      }

      if (removedCount) {
        recordsRemoved(idx, removedCount);
      }
    };
Exemple #4
0
    ['@test modifying the array should also indicate the isDone prop itself has changed'](assert) {
      // NOTE: we never actually get the '@each.isDone' property here.  This is
      // important because it tests the case where we don't have an isDone
      // EachArray materialized but just want to know when the property has
      // changed.
      let each;
      expectDeprecation(() => {
        each = get(ary, '@each');
      });
      let count = 0;

      addObserver(each, 'isDone', () => count++);

      count = 0;
      let item = objectAt(ary, 2);
      set(item, 'isDone', !get(item, 'isDone'));
      assert.equal(count, 1, '@each.isDone should have notified');
    }
Exemple #5
0
    ['@test should be clear caches for computed properties that have dependent keys on arrays that are changed after object initialization'](
      assert
    ) {
      let obj = EmberObject.extend({
        init() {
          this._super(...arguments);
          set(this, 'resources', emberA());
        },

        common: computed('*****@*****.**', function() {
          return get(objectAt(get(this, 'resources'), 0), 'common');
        }),
      }).create();

      get(obj, 'resources').pushObject(EmberObject.create({ common: 'HI!' }));
      assert.equal('HI!', get(obj, 'common'));

      set(objectAt(get(obj, 'resources'), 0), 'common', 'BYE!');
      assert.equal('BYE!', get(obj, 'common'));
    }
Exemple #6
0
 common: computed('*****@*****.**', function() {
   return get(objectAt(get(this, 'resources'), 0), 'common');
 }),
Exemple #7
0
 ['@test `objectAt` returns correct object'](assert) {
   let arr = ['first', 'second', 'third', 'fourth'];
   assert.equal(objectAt(arr, 2), 'third');
   assert.equal(objectAt(arr, 4), undefined);
 }
Exemple #8
0
function find(array, callback, target) {
  let predicate = callback.bind(target);
  let index = findIndex(array, predicate, 0);
  return index === -1 ? undefined : objectAt(array, index);
}
Exemple #9
0
 lastObject: computed(function() {
   return objectAt(this, this.length - 1);
 }).readOnly(),
Exemple #10
0
 firstObject: computed(function() {
   return objectAt(this, 0);
 }).readOnly(),
Exemple #11
0
 return indexes.map(idx => objectAt(this, idx));
Exemple #12
0
  slice(beginIndex = 0, endIndex) {
    let ret = A();
    let length = this.length;

    if (beginIndex < 0) {
      beginIndex = length + beginIndex;
    }

    if (endIndex === undefined || endIndex > length) {
      endIndex = length;
    } else if (endIndex < 0) {
      endIndex = length + endIndex;
    }

    while (beginIndex < endIndex) {
      ret[ret.length] = objectAt(this, beginIndex++);
    }

    return ret;
  },

  /**
    Returns the index of the given object's first occurrence.
    If no `startAt` argument is given, the starting location to
    search is 0. If it's negative, will count backward from
    the end of the array. Returns -1 if no match is found.

    ```javascript
    let arr = ['a', 'b', 'c', 'd', 'a'];

    arr.indexOf('a');       //  0
Exemple #13
0
  /**
    The content array. Must be an object that implements `Array` and/or
    `MutableArray.`

    @property content
    @type EmberArray
    @public
  */

  /**
    Should actually retrieve the object at the specified index from the
    content. You can override this method in subclasses to transform the
    content item to something new.

    This method will only be called if content is non-`null`.

    @method objectAtContent
    @param {Number} idx The index to retrieve.
    @return {Object} the value or undefined if none found
    @public
  */
  objectAtContent(idx) {
    return objectAt(get(this, 'arrangedContent'), idx);
  }
Exemple #14
0
 lastObject: nonEnumerableComputed(function() {
   return objectAt(this, this.length - 1);
 }).readOnly(),
Exemple #15
0
 firstObject: nonEnumerableComputed(function() {
   return objectAt(this, 0);
 }).readOnly(),