Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html data- attributes in ember.js handlebars

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?

like image 388
Bradley Trager Avatar asked Sep 03 '13 17:09

Bradley Trager


People also ask

How add data attribute in HTML?

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.

What is data attribute in HTML element?

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.

What is component in Ember JS?

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.

How does Ember JS work?

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 .


3 Answers

There are different approaches you can do it:

Approach 1

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.

Approach 2

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.

like image 193
intuitivepixel Avatar answered Sep 29 '22 05:09

intuitivepixel


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: -------------------------------
like image 41
Chris Hough Avatar answered Sep 29 '22 05:09

Chris Hough


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.

like image 26
Matic Jurglič Avatar answered Oct 02 '22 05:10

Matic Jurglič