walmartlabs/thorax — pull request #69 by eastridge (2012-12-18 01:37:55+00:00)
Title: Unity

This branch moves `setCollection` and colleciton rendering behavior back into the base `Thorax.View` class so any view is once again capable of rendering a collection. It leaves all of the additional constructs and helpers introduced in Thorax 2 and adds a `Thorax.CollectionHelperView` class that is created when a collection helper is used. Views rendering collections have new options:

- itemView
- itemTemplate
- emptyView
- emptyTemplate

When possible overrides via these options are preffered over subclassing `renderItem`.

Collections may be rendered in one of three ways:

1) Without `setCollection` using a collection helper:

view.myCollection = new MyCollection();
{{#collection myCollection}}{{/collection}}

`item-view`, `item-template`, `empty-view` and `empty-template` will map to the corresponding camel cased view property names.

2) Using `setCollection` and no collection helper:

view.setCollection(new MyCollection());

In this case 1.x behavior will be followed and an item template defaulting to `this.name + "-item"` will be expected. Internally `_collectionSelector` still exists though it now defaults to `[data-collection-element]`. A new `collection-element` helper has been introduced that will generate an element with this attribute.

{{collection-element tag="ul" class="blah"}}

As in 1.x, if no collection element is found the collection element will be the view's `el`.

3) Using `setCollection` and a collection helper:

view.setCollection(new MyCollection());
{{#collection}}{{/collection}}

In this case the `CollectionHelperView` that is generated will be marked as "primary" and the declaring view will not attempt to render the collection since the generated view class will be rendering. Collection related properties will be forwarded from the declaring view to the generated `CollectionHelperView`, for instance:

view.itemContext = myItemContextFunction;
view.setCollection(new MyCollection());

{{#collection item-view="ItemViewClass"}}{{/collection}}

In this case the generated `CollectionHelperView` instance will have an `itemContext` function that is `myItemContextFunction` and an `itemView` of `ItemViewClass`.