54

I try to build a link to a nested route and want to add a class to this link (for twitter bootstrap)

The result should something like this:

< a href="/#/rents/42" class="btn btn-primary btn-small">do something< /a>

First try:

{{#link-to "rent" rent}}

gives me a link to the ressource but I cannot specify a (css) class. In the docs I see that only the title attribute can be specified

Second try:

< a href="/#/rents/{{rend.id}}" class="btn btn-primary btn-small">do something< /a>

is also a bad idea, because Ember will add its helper tags [for automatic updates] in the href.

So what can I do?

Patsy Issa
  • 11,113
  • 4
  • 55
  • 74
user2016429
  • 545
  • 1
  • 4
  • 5

3 Answers3

110

Use:

{{#link-to 'rent' rent class='btn btn-primary btn-small'}}Go to rent{{/link-to}}

As link-to is a view helper.

Daniel Kmak
  • 18,164
  • 7
  • 66
  • 89
sly7_7
  • 11,961
  • 3
  • 40
  • 54
  • 3
    **awesome**! I didn't knew I could add view attributes to the helpers. – user2016429 Jan 27 '13 at 22:45
  • 2
    This does not work in version 1.13.7 of ember anymore as it fails to add the `ember-view` class, without wich it will just be a normal link and reload the page. Using just `class="btn btn-primary etc"` as proposed in Connors answer below works as expected. – irruputuncu Aug 11 '15 at 14:35
  • Thanks for the heads up – sly7_7 Aug 12 '15 at 12:58
10

You can add classes just fine in {{#linkTo}} helpers, you just need to remember not to confuse ember.

Ember may think your class is the routeName of the params, I include the class after both params and routeName and it works fine.

{{#linkTo 'dashboard.screenshots' value.model class='thumbnail'}}
   ........
{{/linkTo}}

Produces

<a id="ember507" class="ember-view thumbnail" href="#/project-2/member-1/task-2/screenshot-30">
   .........
</a>
iConnor
  • 19,997
  • 14
  • 62
  • 97
1

If you want manually construct something from variables - there is {{unbound}} helper in ember.js.

In yor case code will looks like:

<a href="/#/rents/{{unbound rend.id}}" class="btn btn-primary btn-small">
   do something
</a>
iConnor
  • 19,997
  • 14
  • 62
  • 97
sashasimkin
  • 37
  • 1
  • 3