I am trying to do something like this:
{{input value=email type="text" data-type="email"}}
In order to use parsley.js to validate my inputs. (I know email can use type="email") but this is just an example.
But I am seeing that the data-type="email"
is not showing up in the generated HTML.
Is there some way I can add this HTML data- attribute into a handlebars tag?
Adding a data attribute is easy. Any HTML element can have any number of data attributes added to its opening tag. We simply type data- followed by the name of our attribute into the element's opening tag alongside any other attributes we're already using.
The data-* attribute is used to store custom data private to the page or application. The data-* attribute gives us the ability to embed custom data attributes on all HTML elements.
Ember components are used to turn markup text and styles into reusable content. Components consist of two parts: a JavaScript component file that defines behavior, and its accompanying Handlebars template that defines the markup for the component's UI. Components must have at least one dash in their name.
Ember. js uses Handlebars, a lightweight templating engine also maintained by the Ember team. It has the usual templating logic, like if and else , loops and formatting helpers , that kind of stuff. Templates may be precompiled (if you want to cleanly organize them as separate .
There are different approaches you can do it:
You can just reopen Ember.TextField
and define additional attributeBindings
, something like:
Ember.TextField.reopen({
attributeBindings: ['data-type']
});
now this will work:
{{input value=email type="text" data-type="email"}}
Working example.
Define your own custom input field extending ember's Ember.TextField
App.MyTextField = Ember.TextField.extend({
attributeBindings: ['data-type']
});
and use it like this:
{{view App.MyTextField value=email type="text" data-type="email"}}
Working example.
Hope it helps.
If you are working to integrate parsleyjs into your CLI project, here is how I set that up. It has a good number of attributes.
Created initializers/reopen-textfield.js
as:
export default {
name: 'textfield-configuration',
initialize: function() {
Ember.TextField.reopen({
attributeBindings: [
'parsley-type',
'parsley-required-message',
'parsley-type-email-message',
'placeholder'
]
});
}
};
Created initializers/reopen-checkbox.js
as:
export default {
name: 'checkbox-configuration',
initialize: function() {
Ember.Checkbox.reopen({
attributeBindings: [
'parsley-type',
'parsley-required-message',
'parsley-type-email-message',
'parsley-group',
'placeholder'
]
});
}
};
I am using the ember cli project to build my ember application.
Current setup at the time of this post:
DEBUG: -------------------------------
DEBUG: Ember : 1.5.1 vendor.js:15554
DEBUG: Ember Data : 1.0.0-beta.8.2a68c63a vendor.js:15554
DEBUG: Handlebars : 1.3.0 vendor.js:1555
DEBUG: jQuery : 2.1.1
DEBUG: -------------------------------
If you use Ember CLI, you can install a Parsley.js addon. It adds the library files to your project, and allows you to use parsley data attributes.
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