bind-attr
曾經是解決限制的好方法在Ember的渲染中。現在使用HTMLBars Ember建議我們從bind-attr
移開,因爲我們有更強大的方法。
Ember 1.13不推薦使用bind-attr來支持新的語法。 http://emberjs.com/deprecations/v1.x/#toc_bind-attr
工作實例提出的兩種方法都可以在燼捻,這裏的行動中可以看出: https://ember-twiddle.com/38f69f01d2fd994af3b0965f10882005?openFiles=templates.application.hbs%2C
方法1
如果你想要做的內線組合你的車把模板您可以這樣做:
<div class={{concat "fly " isGo}}>Fly now</div>
方法2
否則使用計算的屬性,如:
flyingClass: Ember.computed('isGo', function() {
// return a string with 'fly' and the value of
// isGo. Will be updated if isGo changes.
// Array values are created with space delimited by
// ['className', 'anotherClassName', 'lastClastName'].join(' ');
// => "className anotherClassName lastClassName"
let going = this.get('isGo') ? 'going' : ''
return ['fly', going].join(' ');
})
,然後在你的車把模板:
<div class={{flyingClass}}>Fly now</div>
這兩種方法之間的主要區別取決於你希望你的關注點分離。現在做方法1可能會更容易,但是隨着條件變得更加複雜,您可以隱藏計算屬性中的更多工作。
這在[guides]中有詳細記錄(http://emberjs.com/guides/templates/binding-element-class-names/) – MilkyWayJoe 2014-10-06 14:32:50
http://emberjs.com/guides/templates/binding-element -class-names/ – 2014-10-06 14:36:24