gmac / backbone.epoxy

Declarative data binding and computed models for Backbone
http://epoxyjs.org
MIT License
615 stars 89 forks source link

More than one collection binding per view with different itemView #85

Closed rubenstolk closed 9 years ago

rubenstolk commented 10 years ago

In previous versions of epoxy it was possible to specify the itemView as the 'view' property of a collection. Current version still has legacy support for that, however the problem with the itemView property in general is that we can only use one type itemView per view, even if we want to render more than one collection.

I think the old approach was way better and more flexible.

Also refer to the PR #45, comment by @Prinzhorn

gmac commented 10 years ago

While the new way limits your capacity at present, I generally like the approach a lot better... it was a poor design to have view resources defined in any way on a data entity. I'm still considering whether to support multiple item views, or just do away with multiple binding sources entirely. Multiple sources make a single view take on way too much responsibility.

royslagle commented 9 years ago

Is there a recommended approach to handling this case with the current version? My example is a comment which can have multiple replies as well as multiple attachments - each in a separate collection attribute. Perhaps itemView could handle an array that provides a view for different binding sources?

gmac commented 9 years ago

This has been an outstanding capability for a while since the migration of the item view definition. Thanks for the reminder. I finally buckled down and wrote a feature to address it (see 196da82). Introducing the itemView binding. http://epoxyjs.org/documentation.html#handler-item-view

Long story short: include an itemView binding param along with a collection binding to tell it what view to use. IE: data-bind="collection:$collection,itemView:'myView'... the "myView" is a replacement view constructor, and defined on the parent view that is performing the binding.

royslagle commented 9 years ago

I thought I was going crazy - I noticed this in the docs yesterday and wondered how I had previously missed it! I had created a somewhat similar implementation locally and was planning to create a PR.

After I grabbed the new version it works great - thanks!

DHFW commented 9 years ago

@gmac Ok, I'm not sure if I understand. Is it possible in my main view to have multiple itemViews? like so: itemView: { collectionView1: myCollection, collectionView2: mySecondCollection }

And in the databinding: databind=collection:$myCollection,itemView:'collectionView1' And the second collection similar: databind=collection:$mySecondCollection,itemView:'collectionView2'?

gmac commented 9 years ago

@DHFW: the item view constructors should all attach directly to the parent view. By default, a "collection" binding looks for a property of the parent view called itemView, which is a view constructor. If you want to bind another collection, then define a property on the parent view called anotherItemView, and bind the collection as collection:$anotherCollection,itemView:anotherItemView.

DHFW commented 9 years ago

@gmac Thank you for the fast reply! I just looked at the source code and tried something, and it worked. Indeed by specifying the itemview view as a property of the main view!