Skip to content

Commit

Permalink
Merge pull request #644 from derbyjs/doc-as-array-as-object
Browse files Browse the repository at this point in the history
Document as-array and as-object
  • Loading branch information
craigbeck authored Jul 3, 2024
2 parents c92fd6c + c0a9c30 commit dff7070
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/_sass/custom/custom.scss
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@
}
}
}

h3 > code {
// fix font size of code block in heading
font-size: 1.0rem;
}
36 changes: 36 additions & 0 deletions docs/views/template-syntax/view-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,42 @@ this.container.querySelectorAll('*');
this.modal.close();
```


### `as-array` attribute

Similar to the `as` attribute, a property with the provided name is made available on the component, but in this case the value is an array of multiple references. This is useful in an `each` block, to provide a reference in the controller to every individual item.

```jinja
{{each items as #item, #index}}
<input as-array="itemEditor">{{#item}}</input>
{{/each}}
```

```js
this.itemEditor[index]; // references the Component or DOM element for item at `index`
```

### `as-object` attribute

Similar to the `as-array` attribute, a property with the provided name is made available on the component, but with the value being a map-like object. The second argument to `as-object` specifies the key for each entry.

```jinja
<ul>
{{each _page.items as #item}}
<li as-object="listItems, #item.id">{{#item.name}}</li>
{{/each}}
</ul>
```

```js
this.page.model.set('_page.items', [
{id: 'a', name: 'Item A'},
{id: 'b', name: 'Item B'},
]);

this.listItems.a // references the Component or DOM element for "Item A"
```

### `inherit` attribute

Adding the `inherit` attribute changes the behavior of attribute lookup within the instantiated view. By default, attribute values are only defined in the view that they are passed into explicitly. Passing attribute values through one view into another normally requires manually repeating the attributes, such as `class="{{@class}}"`. `inherit` modifies the default behavior, making all attributes of the parent view instance implicitly available as well. Explicitly providing an attribute will take precedence over `inherit`.
Expand Down

0 comments on commit dff7070

Please sign in to comment.