ember application template classname - ember.js

I'm having a similar problem to this one:
Setting tagName on application template in ember js
While I agree that falling back to a legacy view addon can't be the way to go, here too my bootstrap-based CSS is broken by the enclosing div (the height being not set, to be precise).
Now a different way to achieve what I need is to set the enclosing div's classNames property (if it exists), like it can be done with a component:
export default Ember.Component.extend({
classNames: ['container']
});
Thus I could apply height:100%, and everything would be fine.
Update:
The problem is not the styling of the enclosing div of a component, but the way the main application template behaves. Let me clarify:
application.hbs:
{{outlet}}
Therein is rendered a route's template, e.g. map.hbs:
{{#tab-navigation-container}}
{{top-nav}}
{{tab-contentpane model=model}}
{{tab-navigation map=true}}
{{/tab-navigation-container}}
Now, components/tab-navigation-container.js transforms the enclosing div to include the container CSS class:
import Ember from 'ember';
export default Ember.Component.extend({
classNames: ['container']
});
However, the rendered HTML looks like this:
So, seemingly application.hbs puts another div around the component, and I'm looking for a way to either
remove it (which can only be achieved by a legacy view addon, as explained in the link above) or
apply a className to it.
Can it be done? Thanks!

Okay, this is more of an ugly hack than an actual solution, but for the moment it does the trick. I just fell back to ordinary jQuery DOM manipulation while overwriting the component's didInsertElement event:
export default Ember.Component.extend({
setupFunc: function () {
$('#tab-navigation-container').unwrap();
}.on('didInsertElement')
});
where tab-navigation-container is the component's ID.

You should set tagName and classNames to be your outer most element--this will keep consistent styling. For example, say originally you had:
<section class="container col-md-6">
<div class="col-sm-12 test">
<p>some content...</p>
...
</div>
</section>
You would create your component like this:
export default Ember.Component.extend({
tagName: 'section',
classNames: ['container', 'col-md-6']
});
Then your template can exclude the outer section wrapper
<div class="col-sm-12 test">
<p>some content...</p>
...
</div>
There's really no reason why your CSS should be broken by using components. You can set all the same classes and tags that you were using before and CSS should render just the same.
Let me know if I misunderstood your question.

In Ember 2.7 and below, you can control the top-level tag (even not rendering it), and its CSS classes by modifying your Ember Application as follows:
// app/app.js
App = Ember.Application.extend({
ApplicationView: Ember.Component.extend({
classNames: ['my-custom-classname'],
// Or to omit the tag entirely:
tagName: '',
}),
// ...
});
As seen in this discussion.
This does not require any hacks or a legacy plugin.

Related

Multiple layouts in Ember 2

I need implement next application structure with 3 routes:
localhost/users
localhost/posts
localhost/settings
'users' and 'posts' routes should have basic layout1 with main navbar.
'settings' route should have another layout2 with second navbar.
How can I implement multiple layouts approach with Ember > 2.12?
Can I set a layout name/path for each route or group of routes?
I can think of two possible recommended approaches to this problem. I've used both in different scenarios. You can either:
Use a component to encapsulate each navbar and then present them accordingly in each template
Set the templateName attribute of each route to use the correct template.
The component approach seems to be the easiest/most used in my experience. It also allows you to have differences within your base route template. e.g.:
users.hbs:
{{layout1}}
<h1>Users<h1>
...
posts.hbs:
{{layout1}}
<h1>Posts</h1>
...
While if you use the templateName approach, you are locked into using the same exact template. So, if you need any customization between any page that uses the same layout, you must use a subroute. e.g.:
routes/users.js:
import Ember from 'ember';
export default Ember.Route.extend({
templateName: 'layout1'
});
routes/posts.js:
import Ember from 'ember';
export default Ember.Route.extend({
templateName: 'layout1'
});
templates/layout1.hbs:
<nav>...</nav>
{{outlet}}
A third possible approach, though I don't necessarily recommend it, is to dynamically replace the navbars in the application.hbs template. The application controller/template have access to a special attribute called currentPath, which you can used to create two computed properties (isLayout1 and isLayout2).
With that, this becomes a viable, though like I said, not necessarily recommended solution:
application.hbs:
{{#if isLayout1}}
<nav>layout 1</nav>
{{else}}
<nav>layout 2</nav>
{{/if}}
{{outlet}}

attributeBindings and classBindings not appending to <div> with component helper in Ember 2.1?

I have a widget dashboard app where the individual widgets are displayed using the component helper. A template ends up looking like this:
<div class="panel panel-default">
<div class="panel-body">
{{component model.displayComponent model=model}}
</div>
</div>
A Widget instance will return a path for the displayComponent like: widgets/category/specific-widget.
What I'm finding, in Ember 2.1, is that the attributeBindings and classBindings defined on the component are not being applied. And I think they were being applied back in Ember 1.13.
In other words, in 1.13 think a component defined like this:
// app/components/widgets/category/specific-widget.js
export default Ember.Component.extend({
model: null,
classNames: [ 'widget-content' ],
attributeBindings: [ 'style:style' ],
style: Ember.computed.readOnly('model.contentHeight')
});
I would get a <div id="ember..." style="height:300px" class="ember-view widget-content">...</div>. Now I simply get the default <div id="ember..." class="ember-view">...</div>.
Am I mistaken about the old behavior? If not, is there a way to make this work the old way?
P.S. I'm unable to find the documentation for the component helper anywhere. Is that on the ember.js site?
EDIT: developer error... I had my path to my widget templates named differently (plural) than my widget code. For example: my template was app/templates/components/widgets/categories/widget.hbs but my widget code was app/components/widgets/category/widget.js. Because Ember inserts a component code in there by default, there was no error thrown. If I had made the mistake in the other direction an error would have been thrown.

Getting element by ID in Ember

I am running two ember applications. One has the following component:
import Ember from 'ember';
export default Ember.Component.extend({
tagName: 'a',
click: function() {
Ember.$('#wrapper').toggleClass('toggled');
}
});
and the other one, has this one:
import Ember from 'ember';
export default Ember.Component.extend({
tagName: 'a',
click: function() {
this.$('#wrapper').toggleClass('toggled');
}
});
What I can't understand here is why in one application I select an element by ID using Ember.$('#wrapper') and in the other using this.$('#wrapper').
What is this about? Ember version?
UPDATE
I'm very puzzled, since both components are the same:
{{#show-menu}}
<i class="fa fa-bars"></i>`
{{/show-menu}}`
They are both hamburger menus used to hide a sidebar div, and the #wrapper is an external element.
Since in both cases the #wrapper are external elements, shouldn't just the first case work #Gaurav and #Kevin Jhangiani?
The difference is in the context of the jquery selector.
Ember.$()
is scoped to the entire document, ie, you can access any element on the page.
In contrast,
this.$()
is scoped to the current component or view, and thus you can only access dom elements that are children.
Generally, you should be using this.$ as it will be more performant (since the search space is only child elements). Ember.$ should be reserved for times when you absolutely need to access an element outside of the current context.
Ember.$('#wrapper') will find an element in the page with the id of wrapper.
this.$('#wrapper') will find an elment within the component with the id of wrapper.
If there is any chance that the component you are defining will ever occur more than once in the page, then you should use neither. Edit the appropriate template so that wrapper is a class, not an id. Then use:
this.$('.wrapper')
Since you are essentially just toggling a class, the more "Ember" way of doing this is having a conditional class on your wrapper and toggle a property on your component:
import Ember from 'ember';
export default Ember.Component.extend({
tagName: 'a',
classToggle: false,
click: function() {
this.toggleProperty('classToggle');
}
});
Then, on your DOM element you can have a conditional class:
<div id="wrapper" class="{{if toggleClass "toggled"}}">...</div>
or, if you are using an older version of Ember:
<div id="wrapper" {{bind-attr class="toggleClass:toggled"}}>...</div>
This is a bit more reusable as your component doesn't rely on a DOM element (which can get messy if you want to reuse this component ever).

Component doesn't use template

I want to extend Ember's textarea component.
Therefore I created a file drop-files-textarea.js in app/components with this content:
import Ember from 'ember';
export default Ember.TextArea.extend({
// here comes the new behaviour
})
Now I'd like to add something to it's template. Therefore I created a file called drop-files-textarea.js in app/templates/components with this content:
{{yield}}
<p>added text</p>
This template is never used though.
The Ember Inspector shows me that it uses the inline-template.
What am I doing wrong?
The template is used, but you won't see anything since the TextArea component uses tagName: 'textarea' to wrap the component in a textarea html element, so you get:
<textarea>
<p>added text</p>
aaaa
</textarea>
You may want to create a new component using the textarea component inside or use the value property to set the content of the textarea depending on your use case.
You could also overwrite the tagName, but then you don't get a text area...
export default Ember.TextArea.extend({
value: 'Content of the text area'
})

How do I add classNames to the first element of the page without using a view?

I want to add a wrapper class to the first div element in my page. I used to do this with a view. So, it seems that Ember 2.0 won't support Views anymore. So how can I do that now?
view/application.js:
import Ember from 'ember';
export default Ember.View.extend({
classNames: ['wrapper'],
});
Resulting in the following page:
<body class="ember-application">
<div id="ember573" class="ember-view wrapper">
the rest of my page in this div
</div>
</body>
How is this done now that views are deprecated?
I used css to solve this problem:
body > .ember-view {
padding-left: 240px; //styles for container goes here
}
I don't have a neat solution, but subclassing Ember.Component from inside applications/view.js works.
https://ember-twiddle.com/b15411266f996191605c
Like the others said, the only way to add a class using Ember 2.0 is to use a component on your page. The component has the same properties that the view had. Your page will have a component-only call in the template, like the following:
your-page.hbs
{{your-page-component}}
If you really don't want to have a component on your page, my advice to you would be to add manually a class name in your template:
your-page.hbs
<div class="your-page">
{{outlet}}
</div>
Views are deprecated in ember 2.0. The way to do things from now on is using component and route. You can specify which class name is applied to your component by doing:
export default Ember.Component.extend({
/* Wrap your component in primary class*/
classNames: ['primary'],
/*defined class binding*/
classNameBindings: ['isUrgent'],
isUrgent: true
});
All information regarding on how to customize your component can be found in the ember documentation(click here to find out more)
You're not supposed to use Views now since they're deprecated. Use components instead, example:
import Ember from 'ember';
export default Ember.Component.extend({
classNameBindings: ['functionName'],
functionName: Ember.computed(function() {
// function logic
})
});