Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

throw error when region has more than one element #3341

Closed
wants to merge 33 commits into from

Conversation

erikahlswede
Copy link
Contributor

Proposed changes

  • Throw an error if a region has more than one element

Link to the issue:
#3320

I played around with a warning mechanism...but at the end of the day, it was console.warn. I like the idea of throwing an error in this situation. This way, it is much more clear to the user, rather than having views show up in the wrong spots ☹️

paulfalgout and others added 30 commits March 23, 2017 20:02
Rebuild of CollectionView and children container
Along the lines of `reorder` -> `sort` and `viewComparator`, this change names it what it is and makes them more similar.  It also, and most importantly answers the question, “How do I reapply a filter” which has been a reoccurring question on gitter.
This does two things.  It is more explict what it means when called.  It allows for `_removeChildView` to be called within an `_.each` when the 2nd argument is the index.
after pulling master into next the linter failer for these imports
Backbone does it
`myCollection.filter({ foo: true });`
`myCollection.filter(‘foo’);`

We might as well:
`myCollectionView.setFilter({ foo: true });`
`myCollectionView.setFilter(‘foo’);`

Also `filter` needs to return `this`.
Really we should bail out quick if not viewfilter is set and throw an error if the filter won’t work.

Also `filter` shouldn’t occur if there are no children.  In the normal `render` work flow the `emptyView` would show prior to `filter` and `filter` would never be called.. so this handles when a user calls `filter` and there are no children (possibly because the view has not yet been rendered)

Because `filter` handles no children, `setFilter` does not need to care about `children` or if the view `isRendered`.
Adds:
- `getViewFilter` similar to `getViewComparator`
- `setComparator` similar to `setFilter`
- `removeComparator` similar to `removeFilter`
Until marionettejs#3247 is in, the `_parent` here will skip over the `CollectionView` and trigger on any `View` above the `CollectionView`.  For that reason, currently setting the `_parent` here is a bad idea and users will have to listen to the `emptyRegion` or `emptyRegion.currentView`.  Until a CollectionView can be treated as a parent of a Region we should leave this line out 😿

Additionally the emptyRegion should be destroyed on clean up
If there’s nothing to sort, no need to continue.  This matches filter functionality.
Underscore uses native bind if available, but lodash does not.  This only worked with native bind, so we should continue that.  IE9+ here we come. (or polyfill function.prototype.bind)
recreating `children` would break anyone's reference to the `children` object before a `render`.  This prevents that issue.
This fixes removing a view that isn't in the `children` causing the `children` to be spliced oddly.

Additionally it reuses `removeChildView` in `detachChildView`
Views will be an array of views that were rendered and not filtered out.
Resolves marionettejs#2570
We're using `setFilter` and `removeFilter`.. `getFilter` only makes sense
This also makes `_detachView` more similar to `_attachView`

- [x] Test fails without code changes
Resolves: marionettejs#1579

The use-case here is that often times you want to initialize a behavior with things relative to the initialized view, but the behavior itself is initialized prior to the view's initialization so that it's events can be delegated.

This new event allows for developers to access view properties once the view is ready

```js
var MyBehavior = Marionette.Behavior.extend({
  onInitializeView: function(view) {
    view.$el.addClass('my-behavior');
  }
});
```

Todo:
- [ ] Tests
- [ ] Docs
- [ ] Apply this to marionettejs#3244 upon merge of one or the other
The test following this one is identical except that it is more thorough.
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.06%) to 99.938% when pulling 0650fe2 on erikahlswede:_3320-child-el into 78d1152 on marionettejs:next.

@paulfalgout
Copy link
Member

@marionettejs/marionette-core feeling on whether or not this should be considered breaking?

@erikahlswede
Copy link
Contributor Author

When writing this, I figured it would be considered breaking. There may be cases where multiple elements are found for a region and the app functions perfectly fine (happen to be the first element in the list).

@rafde
Copy link
Member

rafde commented Apr 10, 2017

@paulfalgout It's definitely a major and breaking change. This is best something for Mn 4.

@scott-w
Copy link
Member

scott-w commented Apr 10, 2017

I agree, this is a breaking change. While a developer shouldn't add regions where selectors will hit multiple nodes, the fact is that they can and, thus, probably have.

I'd happy 👍 this for Mn 4 as I think it's the right way to go.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 8925c2a on erikahlswede:_3320-child-el into 78d1152 on marionettejs:next.

@paulfalgout paulfalgout added this to the v4.0 milestone Apr 11, 2017
@paulfalgout paulfalgout force-pushed the next branch 2 times, most recently from 328bb91 to dd36023 Compare May 3, 2017 13:54
@JSteunou
Copy link
Contributor

@erikahlswede could you rebase your branch ?

@paulfalgout
Copy link
Member

FWIW I'm no longer certain this should go in v4. I think perhaps Region has some problems that could be resolved better than an error.

@paulfalgout paulfalgout removed this from the v4.0 milestone Oct 5, 2017
@paulfalgout
Copy link
Member

I think in the end the solution will be handled differently. Too many caveats in the error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants