![](/img/trans.png)
[英]Ember.View.reopen() for binding data-* attribute doesn't work any longer since I updated ember version to 1.13.3
[英]Allow binding to any data-* attribute on an ember component
我正在設計一個庫,我希望允許用戶提供他們可能喜歡的任何data
屬性。
{{my-component data-type='hello' data-name='world'}}
我不知道他們可能想綁定到哪些data
屬性,因此無法將它們添加到attributeBindings
數組中。
有解決方法嗎?
使用組件的didReceiveAtts
鈎子:
didReceiveAttrs(params){
let newAttrs = params.newAttrs;
let attributeBindings = Ember.A();
Object.keys(newAttrs).forEach((attr)=>{
if(attr.indexOf('data-')>= 0){
attributeBindings.pushObject(attr);
}
});
this.set('attributeBindings', attributeBindings);
}
看看樣本旋轉
棄用后更新:
由於didReceiveAttrs
函數的參數已被棄用,因此您需要按以下方式更改代碼:
didReceiveAttrs(){
let attributeBindings = Ember.A();
Object.keys(this).forEach((attr)=>{
if(attr.indexOf('data-')>= 0){
attributeBindings.pushObject(attr);
}
});
this.set('attributeBindings', attributeBindings);
}
見更新的旋轉 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.