|
| 1 | +--- |
| 2 | +title: Deprecation of @ember/array Foundation APIs |
| 3 | +until: "6.0" |
| 4 | +since: "5.8" |
| 5 | +displayId: array.foundations |
| 6 | +--- |
| 7 | + |
| 8 | +The foundational APIs of `@ember/array`, including the `A()` function and the core mixins (`EmberArray`, `MutableArray`, `NativeArray`), are deprecated. These were used to create and extend arrays with Ember's observability. The modern approach is to use native JavaScript arrays, and `TrackedArray` from `tracked-built-ins` when reactivity is needed. |
| 9 | + |
| 10 | +### `A()` Function and Core Mixins |
| 11 | + |
| 12 | +The `A()` function would wrap a native array, making it an `EmberArray`. The `EmberArray` and `MutableArray` mixins could be used to build custom array-like classes. |
| 13 | + |
| 14 | +**Before** |
| 15 | +```javascript |
| 16 | +import { A } from '@ember/array'; |
| 17 | +import EmberObject from '@ember/object'; |
| 18 | +import { MutableArray } from '@ember/array'; |
| 19 | + |
| 20 | +let emberArr = A([1, 2, 3]); |
| 21 | +emberArr.pushObject(4); |
| 22 | + |
| 23 | +const MyArray = EmberObject.extend(MutableArray, { |
| 24 | + // ... implementation ... |
| 25 | +}); |
| 26 | +``` |
| 27 | + |
| 28 | +**After** |
| 29 | +Use native arrays for standard array operations. For arrays that need to be tracked for reactivity in components and other classes, use `TrackedArray` from the `tracked-built-ins` addon. |
| 30 | + |
| 31 | +```javascript |
| 32 | +// For a standard array |
| 33 | +let nativeArr = [1, 2, 3]; |
| 34 | +nativeArr.push(4); |
| 35 | + |
| 36 | +// For a tracked array |
| 37 | +import { TrackedArray } from 'tracked-built-ins'; |
| 38 | +let trackedArr = new TrackedArray([1, 2, 3]); |
| 39 | +trackedArr.push(4); // This mutation is tracked |
| 40 | +``` |
| 41 | + |
| 42 | +### Utility Functions: `isArray` and `makeArray` |
| 43 | + |
| 44 | +These functions helped create and check for arrays. |
| 45 | + |
| 46 | +**Before** |
| 47 | +```javascript |
| 48 | +import { isArray, makeArray } from '@ember/array'; |
| 49 | +let isArr = isArray([]); |
| 50 | +let ensuredArr = makeArray('hello'); |
| 51 | +``` |
| 52 | + |
| 53 | +**After** |
| 54 | +Use native JavaScript equivalents. |
| 55 | + |
| 56 | +```javascript |
| 57 | +// isArray() -> Array.isArray() |
| 58 | +let isArr = Array.isArray([]); |
| 59 | + |
| 60 | +// makeArray() -> custom helper or ensure data is correct |
| 61 | +function ensureArray(value) { |
| 62 | + if (value === null || value === undefined) return []; |
| 63 | + return Array.isArray(value) ? value : [value]; |
| 64 | +} |
| 65 | +let ensuredArr = ensureArray('hello'); |
| 66 | +``` |
| 67 | + |
| 68 | +### Creating Custom Arrays with Proxies |
| 69 | + |
| 70 | +For advanced use cases where you might have created a custom class based on `MutableArray` to add special behaviors to your array, the modern JavaScript equivalent is to use a `Proxy`. A `Proxy` object allows you to intercept and redefine fundamental operations for a target object (like an array), enabling you to create powerful custom wrappers. |
| 71 | + |
| 72 | +**Example: A Logging Array** |
| 73 | + |
| 74 | +Imagine you want to log every time an item is pushed to an array. |
| 75 | + |
| 76 | +**Before**, you might have done this with `MutableArray`: |
| 77 | + |
| 78 | +```javascript |
| 79 | +import EmberObject from '@ember/object'; |
| 80 | +import { MutableArray } from '@ember/array'; |
| 81 | + |
| 82 | +const LoggingArray = EmberObject.extend(MutableArray, { |
| 83 | + // Internal content array |
| 84 | + _content: null, |
| 85 | + |
| 86 | + init() { |
| 87 | + this._super(...arguments); |
| 88 | + this._content = this._content || []; |
| 89 | + }, |
| 90 | + |
| 91 | + // Required primitives |
| 92 | + objectAt(idx) { return this._content[idx]; }, |
| 93 | + get length() { return this._content.length; }, |
| 94 | + |
| 95 | + // Override replace to add logging |
| 96 | + replace(idx, amt, objects) { |
| 97 | + if (amt === 0) { |
| 98 | + console.log(`Adding items: ${objects.join(', ')}`); |
| 99 | + } |
| 100 | + this._content.splice(idx, amt, ...objects); |
| 101 | + this.arrayContentDidChange(idx, amt, objects.length); |
| 102 | + } |
| 103 | +}); |
| 104 | + |
| 105 | +let arr = LoggingArray.create({ _content: [1, 2] }); |
| 106 | +arr.pushObject(3); // Logs: "Adding items: 3" |
| 107 | +``` |
| 108 | + |
| 109 | +**After**, you can achieve the same result more cleanly by wrapping a `TrackedArray` in a `Proxy`. This allows you to add custom behavior while preserving the reactivity provided by `TrackedArray`. |
| 110 | + |
| 111 | +```javascript |
| 112 | +import { TrackedArray } from 'tracked-built-ins'; |
| 113 | + |
| 114 | +function createTrackedLoggingArray(initialItems) { |
| 115 | + // Start with a TrackedArray instance |
| 116 | + const trackedArr = new TrackedArray(initialItems); |
| 117 | + |
| 118 | + return new Proxy(trackedArr, { |
| 119 | + get(target, prop, receiver) { |
| 120 | + // Intercept the 'push' method |
| 121 | + if (prop === 'push') { |
| 122 | + return function(...args) { |
| 123 | + console.log(`Adding items via push: ${args.join(', ')}`); |
| 124 | + // Call the original push method on the TrackedArray |
| 125 | + // This will trigger reactivity automatically. |
| 126 | + return target.push(...args); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // Forward all other property access and method calls to the TrackedArray |
| 131 | + const value = Reflect.get(target, prop, receiver); |
| 132 | + return typeof value === 'function' ? value.bind(target) : value; |
| 133 | + }, |
| 134 | + |
| 135 | + set(target, prop, value, receiver) { |
| 136 | + // Intercept direct index assignment |
| 137 | + if (!isNaN(parseInt(prop, 10))) { |
| 138 | + console.log(`Setting index ${prop} to ${value}`); |
| 139 | + } |
| 140 | + // Forward the set operation to the TrackedArray to trigger reactivity |
| 141 | + return Reflect.set(target, prop, value, receiver); |
| 142 | + } |
| 143 | + }); |
| 144 | +} |
| 145 | + |
| 146 | +// In a component: |
| 147 | +class MyComponent { |
| 148 | + loggingArray = createTrackedLoggingArray([1, 2]); |
| 149 | + |
| 150 | + addItem() { |
| 151 | + this.loggingArray.push(3); // Logs and triggers an update |
| 152 | + } |
| 153 | + |
| 154 | + updateItem() { |
| 155 | + this.loggingArray[0] = 'new value'; // Logs and triggers an update |
| 156 | + } |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +This `Proxy` approach is very powerful. By wrapping a `TrackedArray`, you can layer in custom logic while letting it handle the complexities of reactivity. This is the recommended pattern for creating advanced, observable array-like objects in modern Ember. |
0 commit comments