Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Ember handlebars property binding


May 09, 2021 Ember.js Reference documents



Simply put, property binding is actually using handlebars expressions directly within an HTML tag (used in the "" of a label). The value of handlebars can be used directly as the value of a property in an HTML tag.

Preparation: ember generate route binding-element-attributes

1, binding string

  1. <div id="logo">
  2. <img src={{model.imgUrl}} alt='logo' />
  3. </div>

Add test data route:binding-element-attributes

  1. import Ember from 'ember';
  2. export default Ember.Route.extend({
  3. model: function() {
  4. return { imgUrl: 'http://i1.tietuku.com/1f73778ea702c725.jpg' };
  5. }
  6. });

After running, the template compiles into code like this:

  1. <div id="logo">
  2. <img alt="logo" src="http://i1.tietuku.com/1f73778ea702c725.jpg" rel="external nofollow" >
  3. </div>

You {{model.imgUrl}} property is src bound as a string.

2, binding the Boolean value

During development, we often judge whether to add cSS classes to a label based on a value, or to determine whether a button is available based on a value, and so on... S o how does ember do it? The following code, for example, shows checkbox button is available based on the value of the bound property isEnable. isEnable

  1. {{! 当isEnable为true时候,disabled为true,不可用;否则可用}}
  2. <input type='checkbox' disabled={{model.isEnable}}>

If the value set in the route is true the rendered HTML is as follows:

  1. <input type="checkbox" disabled="">

Otherwise

  1. <input type="checkbox">

3, binding data-xxx properties

By default, ember is not bound data-xxx as data-xxx. For example, the binding results below will not get what you expect.

  1. {{! 绑定到data-xxx这种属性需要特殊设置}}
  2. {{#link-to 'photo' data-toggle='dropdown'}} link-to {{/link-to}}
  3. {{input type='text' data-toggle='tooltip' data-placement='bottom' title="Name"}}

The template is rendered and the following HTML code is obtained

  1. <a id="ember455" href="/binding-element-attributes" class="ember-view active"> link-to </a>
  2. <input id="ember470" title="Name" type="text" class="ember-view ember-text-field">

You can see that the properties of data-xxx are missing!!! D ata-xxx is now used in many data-xxx such as bootstrap S o what do we do... Y ou can specify the corresponding rendering components Ember.LinkComponent and Ember.TextField in the view. Execute the command to get the view file:
ember generate view binding-element-attributes
Manual binding in view, as follows:

  1. // app/views/binding-element-attributes.js
  2. import Ember from 'ember';
  3. export default Ember.View.extend({
  4. });
  5. // 下面是官方给的代码,但很明显看出来这种使用方式不是2.0版本的!!
  6. // 2.0版本的写法还在学习中,后续在补上,现在为了演示模板效果暂时这么写!毕竟本文的重点还是在模板属性的绑定上
  7. // 绑定input
  8. Ember.TextField.reopen({
  9. attributeBindings: ['data-toggle', 'data-placement']
  10. });
  11. // // 绑定link-to
  12. Ember.LinkComponent.reopen({
  13. attributeBindings: ['data-toggle']
  14. });

The results obtained after rendering are in line with expectations. Get the HTML code below

  1. <a id="ember398" href="/binding-element-attributes" data-toggle="dropdown" class="ember-view active">link-to</a>
  2. <input id="ember414" title="Name" type="text" data-toggle="tooltip" data-placement="bottom" class="ember-view ember-text-field">

You can see that the properties of data-xxx are rendered normally to HTML.

This article introduces several commonly used property binding methods, very practical! B ut a little regret that I have limited ability to understand how the Ember2.0 the code is now based on personal understanding of the specified component code in view, the official tutorial is not Ember2.0 binding-element-attributes.js code of this file is a bit strange ... I hope readers will not hesitate to teach!
The full code of the blog post is placed in Github (the blog post has been modified several times, and the code on the blog post may be different from the github code, but the impact is small!). I f you think the blog post is a bit useful to you, please give me a star on the star project. Yours is definitely the biggest motivation for me!!