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

Region: $el set in constructor might be resolved wrongly #3389

Open
ardi-n opened this issue Jun 20, 2017 · 6 comments
Open

Region: $el set in constructor might be resolved wrongly #3389

ardi-n opened this issue Jun 20, 2017 · 6 comments
Milestone

Comments

@ardi-n
Copy link

ardi-n commented Jun 20, 2017

Description

  1. Having a server-generated html structure of a collection I want to initialize it using a CollectionView with any needed nested views.
  2. Because child views are identical they have identical region definitions.
  3. Regions are initialized before el/$el are resolved for a view.
  4. Thus region's $el is resolved wrongly in constructor - parentEl is not yet defined so region's $el is being searched in a whole document resulting in a jQuery collection of as many elements as there are in a list.
  5. I can't use region's $el to i.e. search within it for existing html that should be initialized with nested views.
  6. Naturally region's $el is fixed after show() is called.

Expected behavior

Region's $el should be usable if it is set in constructor. Regions could be initialized after a view calls setElement.

Environment

  1. Marionette version: 3.3.1
  2. Backbone version: 1.3.3
@paulfalgout
Copy link
Member

I'm pretty sure I follow this, but a fiddle would help.

This would require regions be setup after initialize as well.

Personally I think that the ideal fix would be for regions to never be initialized unless getRegion is called, so they wouldn't be in the constructor at all.

Then I also think that we should remove the DOM lookup from the region altogether. I think ideally the view should pass the el to the region rather than the whole parentEl business.

We might be able to make the getRegion a lazy evaluation without being breaking.

@paulfalgout
Copy link
Member

A pretty poor work around for the moment would be to:

regions() {
   this.setElement(this.el);

  return {
    region: '.selector'
  }
}

@ardi-n
Copy link
Author

ardi-n commented Jun 20, 2017

I'll try to deliver a fiddle tomorrow. But this workaround should indeed work.

@ardi-n
Copy link
Author

ardi-n commented Jun 21, 2017

Here's a demo: https://jsfiddle.net/9732v9cx/
The suggested workaround does not work. Only if you call _ensureElement() on a region its $el is fixed.

@paulfalgout
Copy link
Member

paulfalgout commented Jun 21, 2017

ah the problem in the workaround is that this.el isn't set until the backbone.view constructor call.. well actually not until the setElement call :-) in this case.. but if you change it to this.getOption('el') it should work.

@JSteunou
Copy link
Contributor

Personally I think that the ideal fix would be for regions to never be initialized unless getRegion is called, so they wouldn't be in the constructor at all.

@paulfalgout that's kind of what I did in my autoregion plugin, and indeed that's way better https://github.com/JSteunou/marionette.autoregion

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

No branches or pull requests

3 participants