Beispiel #1
0
    ['@test should notify when called with diff length items'](assert) {
      arrayContentWillChange(obj, 0, 2, 1);
      assert.deepEqual(observer._before, [obj, 0, 2, 1]);

      arrayContentDidChange(obj, 0, 2, 1);
      assert.deepEqual(observer._after, [obj, 0, 2, 1]);
    }
Beispiel #2
0
    ['@test should notify when passed lengths are different'](assert) {
      arrayContentWillChange(obj, 0, 1, 2);
      assert.equal(obj._after, 0);

      arrayContentDidChange(obj, 0, 1, 2);
      assert.equal(obj._after, 1);
    }
Beispiel #3
0
    ['@test should notify array observers when called with no params'](assert) {
      arrayContentWillChange(obj);
      assert.deepEqual(observer._before, [obj, 0, -1, -1]);

      arrayContentDidChange(obj);
      assert.deepEqual(observer._after, [obj, 0, -1, -1]);
    }
Beispiel #4
0
    ['@test should notify observers when call with no params'](assert) {
      arrayContentWillChange(obj);
      assert.equal(obj._after, 0);

      arrayContentDidChange(obj);
      assert.equal(obj._after, 1);
    }
Beispiel #5
0
    ['@test removing array observer should disable'](assert) {
      removeArrayObserver(obj, observer);
      arrayContentWillChange(obj);
      assert.deepEqual(observer._before, null);

      arrayContentDidChange(obj);
      assert.deepEqual(observer._after, null);
    }
Beispiel #6
0
    // API variation that included items only
    async ['@test should not notify when passed lengths are same'](assert) {
      arrayContentWillChange(obj, 0, 1, 1);
      await runLoopSettled();

      assert.equal(obj._after, 0);

      arrayContentDidChange(obj, 0, 1, 1);
      await runLoopSettled();

      assert.equal(obj._after, 0);
    }
Beispiel #7
0
    ['@test should notify observers of []'](assert) {
      obj = DummyArray.extend({
        enumerablePropertyDidChange: emberObserver('[]', function() {
          this._count++;
        }),
      }).create({
        _count: 0,
      });

      assert.equal(obj._count, 0, 'should not have invoked yet');

      arrayContentWillChange(obj, 0, 1, 1);
      arrayContentDidChange(obj, 0, 1, 1);

      assert.equal(obj._count, 1, 'should have invoked');
    }
Beispiel #8
0
*/
const TestArray = EmberObject.extend(EmberArray, {
  _content: null,

  init() {
    this._content = this._content || [];
  },

  // some methods to modify the array so we can test changes.  Note that
  // arrays can be modified even if they don't implement MutableArray.  The
  // MutableArray is just a standard API for mutation but not required.
  addObject(obj) {
    let idx = this._content.length;
    arrayContentWillChange(this, idx, 0, 1);
    this._content.push(obj);
    arrayContentDidChange(this, idx, 0, 1);
  },

  removeFirst() {
    arrayContentWillChange(this, 0, 1, 0);
    this._content.shift();
    arrayContentDidChange(this, 0, 1, 0);
  },

  objectAt(idx) {
    return this._content[idx];
  },

  length: computed(function() {
    return this._content.length;
  }),
Beispiel #9
0
    If you are implementing an object that supports `EmberArray`, call this
    method just after the array content changes to notify any observers and
    invalidate any related properties. Pass the starting index of the change
    as well as a delta of the amounts to change.

    @method arrayContentDidChange
    @param {Number} startIdx The starting index in the array that did change.
    @param {Number} removeAmt The number of items that were removed. If you
      pass `null` assumes 0
    @param {Number} addAmt The number of items that were added. If you
      pass `null` assumes 0.
    @return {EmberArray} receiver
    @public
  */
  arrayContentDidChange(startIdx, removeAmt, addAmt) {
    return arrayContentDidChange(this, startIdx, removeAmt, addAmt);
  },

  /**
    Iterates through the array, calling the passed function on each
    item. This method corresponds to the `forEach()` method defined in
    JavaScript 1.6.

    The callback method you provide should have the following signature (all
    parameters are optional):

    ```javascript
    function(item, index, array);
    ```

    - `item` is the current item in the iteration.