marionettejs/backbone.marionette — pull request #1997 by jasonLaster (2014-10-15 15:35:06+00:00)
Title: Create view, region, and regionManager parent references

**tl;dr:** Lets add parent references to our view layer so that we can traverse up the region trees.

```js
view._region // => Region or CollectionView
region._owner // => RegionManager
regionManager._owner // => Layout or Application
```

----

### Overview

Marionette is the best platform for building rich layouts because of its amazing support for nesting layouts and lists and the flexibility of regions.

With that said, one of the themes to emerge out of the 2.x work is that while support for *building* region trees is great, support for communicating up and down the tree is less less than ideal.

When communication breaks down, other patterns emerge as crutches. Two patterns that have become too prevelant are triggering events on shared models and the creation of over-specific channels for one off events. Communicating via models or channels are not inherently bad, but abusing them is.

What Marionette needs is a great way communicating some events down the tree and some events up the tree.

@jmeas' work for `getNestedViews` will enable events to bubble down. The first event to be triggered will be `onAttach` and `onDetatch`

@samccone started work on a `regionEvents` feature, which would allow layouts to listen to children events. This was going to be implemented with proxied events. While this solution works, I think a better solution would be to maintain parent references. e.g. `view._region //=> region`.

#### Parent References
Parent references in the abstract will make it possible to traverse up our region trees. With parent references, it will be possible to start at a leaf node (View) and travel all the way up to the root (App).

##### Why is this a good thing?
+ It is the kind of backbone architecture I'd expect from a great layout manager.
+ It will make event bubbling trivial to implement
+ It will greatly improve the inspector and debugging.

##### Story Time
Last thursday I was helping a friend debug a weird view event bug, after twenty minutes, we realized the view was a zombie and no longer attached to the region tree or the dom. If I could have checked the _region property we'd be done in a minute. If the Inspector could have a link to the parent container that'd be the shit!

##### Another way to look at Parent References
Few people know that Backbone keeps a `_collection` property on the model so that it's easy to reference the collection that a model belongs to. The `view._region`, `region._owner`, and `regionManager._owner` fields are similar back-references for internal purposes.

#### Counter-Arguments

I realize this could be seen as a controversial change even though it's internal to marionette. Curious to hear your thoughts.

Some possible arguments I can come up with are:

+ We don't *actually* want to communicate events up the tree
+ Developers will find a way to abuse the parent references for terrible anti-pattern practices
+ We will add complexity, which will become a memory management nightmare

I think these arguments though are quickly disputed:
1. There are lots of events that we parent views listen to now. The problem is that setting up these event handlers is really round-about.
2. While some developers could use these internal properties for evil, they're private and they're strongly discouraged.
3. We do add complexity by managing parent references. But the view layer is the main reason we exist as a framework, so we better do it right and if we don't someone else will.

---

Curious to hear your thoughts.
Sorry for the book. I channeled my inner James Smith. :heart: