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?
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).
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.
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.
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.
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.
@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);
}
});
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With