ember.js icon indicating copy to clipboard operation
ember.js copied to clipboard

ability to avoid creating the dummy parent div which has the class "ember-view"

Open abousamra opened this issue 12 years ago • 18 comments

Why ember enforces creating a parent div for any created components or view ?

abousamra avatar Oct 08 '14 09:10 abousamra

actually, you can avoid the "creation of a parent div" if you set the tagName property on your Component/View manually. So, if you want to have an unordered list view, you'll do something like

var MyListView = Ember.View.extend({
  tagName: 'ul'
});

and in your handlebars you're just adding <li></li> elements.

<li>
I'am so
</li>
<li>
unorderded
</li>

herom avatar Oct 08 '14 09:10 herom

@herom thanks for you quick response.

But what if i want to avoid creating parent tag at all, i want to depend on my handlebars only ?

abousamra avatar Oct 08 '14 09:10 abousamra

I don't think that this is possible as the view would not know its boundaries (which is clear if you see the view as a representation of an actual DOM element). What's the particular use case you're trying to cover? Could you perhaps provide a jsbin?

herom avatar Oct 08 '14 10:10 herom

My use case is; I am trying to create a component and found that ember added a parent div which wrapping my component html and this broke my css .
`


Hi there

` http://emberjs.jsbin.com/zufadazogeda/1/edit?html,js,output

The solution i did to define a javascript component class and override the tagName and remove the top level tag from the handlebars to the required output to be like this `

Hi there

` http://emberjs.jsbin.com/cudezayutoxo/1/edit

This pattern happens with me many times. and this is a working solution but I am not sure if this the best practice or not. I am wondering if we have a better solution instead of creating the javascript component or have the option to avoid creating this parent div and take my handlebars without adding the parent div.

abousamra avatar Oct 08 '14 10:10 abousamra

I don't think that there is some "better" practice to achieve this, as views and components are also targets for actions, etc. so how would Ember detect if the code is related to a Component/View or is just "some random DOM markup" and distinguish between this possibilities? You need to tell Ember what's this.$() and therefore which is its DOM representation (at least that's what I know so far).

herom avatar Oct 08 '14 10:10 herom

As I remember in backbonejs you can specify the element that the view responsible for via a normal jquery selector otherwise a new element will be created.

anyway this works for now and hope if they can improve this in the future or not.

thanks a lot man.

abousamra avatar Oct 08 '14 11:10 abousamra

No problem - you're welcome :+1: :smiley:

herom avatar Oct 08 '14 11:10 herom

TagName null

stefanpenner avatar Oct 08 '14 12:10 stefanpenner

@stefanpenner If use tagName: null the HTML defaults to the normal div tag. If I use tagName: 'null' instead I get . Is this a component issue (which is where I am doing this) or did I misunderstand your direction to set the tagName to null?

notmessenger avatar Oct 08 '14 13:10 notmessenger

@stefanpenner and @notmessenger I have just tried it. When using tagName: null you tell ember to create div which is the default value.

and If you use tagName: 'null' it will create a tag <null > </null>

But if you want to avoid creating the tag at all you can keep it empty like tagName: '' and this worked perfectly with me. and @herom btw this is the solution for my problem above.

abousamra avatar Oct 08 '14 13:10 abousamra

cool thing, didn't know about that @abousamra - thanks for update and sharing :+1:

herom avatar Oct 08 '14 13:10 herom

The tagName: '' technique doesn't appear to be documented or under test, and from what I can tell when used internally is always coupled with 'isVirtual: true.' I'd consider it a private API, personally, however useful.

endash avatar Oct 08 '14 13:10 endash

@endash good point and should be considered, thanks.

abousamra avatar Oct 08 '14 13:10 abousamra

@endash correct

stefanpenner avatar Oct 08 '14 14:10 stefanpenner

and If you use tagName: 'null' it will create a tag

oops, answered from my phone '' as endash pointed out is correct, and likely coupled with isVirtual (if that is what you need.

Although isVirtual may not be what you want, it depends on the use-case

stefanpenner avatar Oct 08 '14 14:10 stefanpenner

Man I used to be such a cowboy developer. Now I go, "Not documented? Not tested? I have no idea what this is actually doing today, or what it might do tomorrow. PASS!"

endash avatar Oct 08 '14 14:10 endash

FWIW this technique at least in 1.7.0 will potentially seriously b0rk the children of a collectionview

endash avatar Oct 08 '14 16:10 endash

tagName: "", fixed it for me

sajaldas avatar Nov 04 '17 14:11 sajaldas