|
| 1 | +--- |
| 2 | +title: Array modification |
| 3 | +--- |
| 4 | +By default, Ractive will intercept the *mutator methods* (`pop`, `push`, `shift`, `unshift`, `splice`, `sort` and `reverse`) of arrays that it [depends on](dependants) for more convenient data binding. |
| 5 | + |
| 6 | +Consider the following: |
| 7 | + |
| 8 | +```html |
| 9 | +<ul> |
| 10 | + \{{#list}} |
| 11 | + <li>\{{this}}</li> |
| 12 | + \{{/list}} |
| 13 | +</ul> |
| 14 | +``` |
| 15 | + |
| 16 | +```js |
| 17 | +list = [ 'a', 'b', 'c' ]; |
| 18 | + |
| 19 | +ractive = new Ractive({ |
| 20 | + el: myContainer, |
| 21 | + template: myTemplate, |
| 22 | + data: { list: list } |
| 23 | +}); |
| 24 | + |
| 25 | +list.push( 'd' ); // adds a new list item - <li>d</li> |
| 26 | +``` |
| 27 | + |
| 28 | +You can disable this behaviour by passing in `modifyArrays: false` as an {{{createLink 'initialisation Options'}}} |
| 29 | + |
| 30 | + |
| 31 | +## How it works |
| 32 | + |
| 33 | +Don't worry, we're not modifying `Array.prototype`. (What do you think this is, [Ember](http://emberjs.com/guides/configuring-ember/disabling-prototype-extensions/)? :-) |
| 34 | + |
| 35 | +Instead, we're using a technique called [prototype chain injection](http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/#wrappers_prototype_chain_injection), which allows us to remain performant and memory-efficient without mucking about extending native objects. |
| 36 | + |
| 37 | +This uses the non-standard (but very unlikely to disappear!) `__proto__` property. That might seem kludgy, but if [Mike Bostock thinks it's okay](http://bost.ocks.org/mike/selection/#subclass) then that's good enough for us. |
| 38 | + |
| 39 | +Older browsers (I'm looking at you, IE8) don't support `__proto__` - in these cases, we simply add the wrapped methods as properties of the array itself. |
| 40 | + |
| 41 | +As well as intercepting or wrapping the mutator methods, Ractive adds a (non-enumerable, in modern browsers) `_ractive` property to arrays, which contains information about which Ractive instances depend on the array, and which keypaths it is assigned to. |
| 42 | + |
| 43 | + |
| 44 | +## Hygiene |
| 45 | + |
| 46 | +When an array is no longer depended on by any Ractive instances, we can revert it to its normal state - resetting its prototype (if we used prototype chain injection) or deleting the wrapped methods (if we're in a crap browser), and removing the `_ractive` property. |
| 47 | + |
| 48 | + |
| 49 | +## Performance and UI benefits |
| 50 | + |
| 51 | +As well as convenience, using arrays like this helps Ractive make smart decisions about how to update the DOM. Continuing the example above, compare these two alternative methods of inserting a new item at the *start* of our list: |
| 52 | + |
| 53 | +```js |
| 54 | +// at the moment, list = [ 'a', 'b', 'c', 'd' ] |
| 55 | + |
| 56 | +// 1. Reset the list: |
| 57 | +ractive.set( 'list', [ 'z', 'a', 'b', 'c', 'd' ] ) |
| 58 | + |
| 59 | +// 2. Use `unshift`: |
| 60 | +list.unshift( 'z' ); |
| 61 | +``` |
| 62 | + |
| 63 | +In the first example, Ractive will see that the content of the first list item has changed from `'a'` to `'z'`, and that the second has changed from `'b'` to `'a'`, and so on, and update the DOM accordingly. It will also see that there is now a fifth item, so will append `<li>d</li>` to the list. |
| 64 | + |
| 65 | +In the second example, Ractive will understand that all it needs to do is insert `<li>z</li>` at the start of the list, leaving everything else untouched. |
| 66 | + |
| 67 | +This is particularly important if you're using {{{createLink 'transitions'}}}, as it will be obvious to the user which elements are being added and removed. |
| 68 | + |
| 69 | +Note that if `list.unshift('z')` isn't an option, you could use {{{createLink 'ractive.merge()'}}} to achieve the same effect. |
0 commit comments