Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember JS/Handlebars view helper

Currently, if we define our view as {{#view App.myView}}, ember/handlebars will wrap the view element inside a <div id="ember-1234" class='ember-view'>.

Is there a way to stop this?

like image 237
trivektor Avatar asked Mar 16 '12 05:03

trivektor


People also ask

How do I create a helper in Ember?

To implement the helper, we write a JavaScript function that takes its arguments as an array. This is because helpers can also receive named arguments, which we'll discuss next. import { helper } from '@ember/component/helper'; function substring(args) { let [string, start, end] = args; return string.

What is handlebars in Ember JS?

Ember uses the Handlebars templating library to power your app's user interface. Handlebars templates contain static HTML and dynamic content inside Handlebars expressions, which are invoked with double curly braces: {{}} . Dynamic content inside a Handlebars expression is rendered with data-binding.


3 Answers

You probably want to set tagName as ''.

App.MyView = Em.View.extend({
    tagName: ''
});

At least it stops wrapping inner contents.

like image 122
Stargazer Avatar answered Oct 16 '22 20:10

Stargazer


If you want to customize view element's id, you can use:

{{#view App.myView id="my-id"}}
like image 39
Mike Aski Avatar answered Oct 16 '22 22:10

Mike Aski


I usually think my views as wrapper. For example, if the initial given html code is :

<div class="item"><p>my stuff></p></div>

Then I create a view with a tagName property as "div" (which is default), and classNames property as "item". This will render, with proper handlebars template :

{#view App.myView}
    <p>my stuff></p>
{/view}

-> render as

<div id="ember-1234" class="ember-view item">
    <p>my stuff></p>
</div>

Then if you need to have your proper ID on this div, you could define the "elementId" property on your view class (before create()). (@see source code)

like image 2
dogawaf Avatar answered Oct 16 '22 22:10

dogawaf