ember.js
ember.js copied to clipboard
[Feature] ArrayProxy should be iterable
Until now it is quite cumbersome to work with ArrayProxy, because a consumer often needs to know that he is not working with a regular array but with the Prox object. However, this knowledge should not be necessary in many cases. Optimal would be, if ArrayProxy would implement the array interface completely, but if it is at least iterable, this would be already a big help.
Recently also no-array-prototype-extensions rule is active. The ways listed there to solve the problem do not apply to the proxy object.
For example, the proxy object can not be used with the spread operator:
import { A } from '@ember/array';
import ArrayProxy from '@ember/array/proxy';
import { module, test } from 'qunit';
module('Unit | Utility | array-proxy', function () {
test('ArrayProxy can be used with spread operator', function (assert) {
const pets = ['dog', 'cat', 'fish'];
const ap = ArrayProxy.create({ content: A(pets) });
assert.deepEqual(pets, [...ap]);
});
});
this will lead to the following error:
not ok 1 Chrome 104.0 - [3 ms] - Unit | Utility | array-proxy: ArrayProxy can be used with spread operator
---
actual: >
null
stack: >
TypeError: ap is not iterable
at Object.<anonymous> (http://localhost:7357/assets/tests.js:13933:34)
message: >
Died on test #1: ap is not iterable
at Object.<anonymous> (http://localhost:7357/assets/tests.js:13926:21)
negative: >
false
browser log: |
...
I think we can close this one.
There is work to make regular arrays work with Ember and template tracking. ArrayProxy will be deprecated eventually.
More background: https://github.com/emberjs/rfcs/pull/669 https://github.com/emberjs/rfcs/pull/848 https://github.com/emberjs/rfcs/pull/812
Good call, @sandstrom – yeah, we are going to remove ArrayProxy, so we're definitely not going to add more behavior to it. @maconfr if you want to get that behavior, you can use TrackedArray or implement your own ArrayProxy-like type using arrays, the tracked primitives, and native Proxy.