Binding Element Attributes Edit Page


In addition to normal text, you may also want to have your templates contain HTML elements whose attributes are bound to the controller.

For example, imagine your controller has a property that contains a URL to an image:

1
2
3
<div id="logo">
  <img src={{logoUrl}} alt="Logo">
</div>

This generates the following HTML:

1
2
3
<div id="logo">
  <img src="http://www.example.com/images/logo.png" alt="Logo">
</div>

Binding multiple values

You can also bind multiple values to a single attribute: handlebars <div class="{{priority}} {{placement}}"></div>

Binding boolean values

If you use data binding with a Boolean value, it will add or remove the specified attribute. For example, given this template:

1
<input type="checkbox" disabled={{isAdministrator}}>

For further control over how these values are applied, see the inline-if syntax documentation on the Conditionals page.

Adding data attributes

By default, view helpers do not accept data attributes. For example

1
2
3
{{#link-to "photos" data-toggle="dropdown"}}Photos{{/link-to}}

{{input type="text" data-toggle="tooltip" data-placement="bottom" title="Name"}}

renders the following HTML:

1
2
3
<a id="ember239" class="ember-view" href="#/photos">Photos</a>

<input id="ember257" class="ember-view ember-text-field" type="text" title="Name">

There are two ways to enable support for data attributes. One way would be to add an attribute binding on the view, e.g. Ember.LinkView or Ember.TextField for the specific attribute:

1
2
3
4
5
6
7
export default Ember.LinkView.reopen({
  attributeBindings: ['data-toggle']
});

export default Ember.TextField.reopen({
  attributeBindings: ['data-toggle', 'data-placement']
});

Now the same handlebars code above renders the following HTML:

1
2
3
4
<a id="ember240" class="ember-view" href="#/photos" data-toggle="dropdown">Photos</a>

<input id="ember259" class="ember-view ember-text-field"
       type="text" data-toggle="tooltip" data-placement="bottom" title="Name">

You can also automatically bind data attributes on the base view with the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
export default Ember.View.reopen({
  init: function() {
    this._super();
    var self = this;

    // bind attributes beginning with 'data-'
    Em.keys(this).forEach(function(key) {
      if (key.substr(0, 5) === 'data-') {
        self.get('attributeBindings').pushObject(key);
      }
    });
  }
});

Now you can add as many data-attributes as you want without having to specify them by name.