Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set custom data- attributes on {{#linkTo}} helper <a> tag

How can I set custom data- attribute on the {{#linkTo}} helper? I want use this:

{{#linkTo "foo" data-toggle="dropdown"}}foo{{/linkTo}}

and the result should look like this:

<a id="ember323" class="ember-view active"  data-toggle="dropdown" href="#/foo/123">foo</a>

but it looks like:

<a id="ember323" class="ember-view active"  href="#/foo/123">foo</a>

How can I do this?

like image 800
Lux Avatar asked Aug 23 '13 10:08

Lux


People also ask

How add custom data attribute in HTML?

HTML data-* Attribute The data-* attribute gives us the ability to embed custom data attributes on all HTML elements. The stored (custom) data can then be used in the page's JavaScript to create a more engaging user experience (without any Ajax calls or server-side database queries).

What are custom data attributes?

Custom data attributes are intended to store custom data private to the page or application, for which there are no more appropriate attributes or elements. These attributes are not intended for use by software that is independent of the site that uses the attributes.

How do you set data attribute using JS?

You can use getAttribute() and setAttribute() in JavaScript to get and set the value of different data attributes. The getAttribute method will either return null or an empty string if the given attribute does not exist. Here is an example of using these methods: var restaurant = document.

Can HTML tags have custom attributes?

Custom attributes are attributes that are not part of the standard HTML5 attributes but are explicitly created. They allow us to add our own information to HTML tags. These are not specific and can be used with all the HTML elements.


2 Answers

A way you could do this is to extend your Ember.LinkComponent to be aware of the new attribute:

Ember.LinkComponent.reopen({
  attributeBindings: ['data-toggle']
});

And then you can use it in your {{#link-to}} helper:

{{#link-to 'foo' data-toggle="dropdown"}}Foo{{/link-to}}

This will result in:

<a id="ember262" class="ember-view active" href="#/foo" data-toggle="dropdown">Foo</a>

And since attributeBindings is an array your can put multiple attributes there that you might need:

attributeBindings: ['data-toggle', 'foo', 'bar']

Hope it helps.

like image 142
intuitivepixel Avatar answered Oct 21 '22 21:10

intuitivepixel


@intuitivepixel

Thanks

U helped a lot. With that Information I've played arround with the LinkView and was able to find a generic solution:

Em.LinkView.reopen({
    init: function() {
        this._super();
        var self = this;
        Em.keys(this).forEach(function(key) {
            if (key.substr(0, 5) === 'data-') {
                self.get('attributeBindings').pushObject(key);
            }
        });
    }
});
like image 34
Lux Avatar answered Oct 21 '22 23:10

Lux