我有一个组件如下
{{#md-collection content=model as |item|}}
<div class='collection-item'>
<img src="{{item.url}}" class="asset-thumbnail" />
<div class="asset-url">
{{item.url}}
</div>
<div class="secondary-content">
{{#copy-button
clipboardText=item.url
class="btn"
success="successfulCopy"
}}
{{fa-icon "chain" title="Copy to Clipboard"}} {{unless copied "Copy Link" "Copied"}}
{{/copy-button}}
{{confirmation-link
title="Delete"
action=(route-action "deleteAsset" item)
icon="trash"
message="Are you sure you want to delete this asset?"
confirmButtonText="Yes, Delete Asset"
confirmButtonColor="#FF6666"
classNames="btn delete"}}
</div>
</div>
{{/md-collection}}
它有控制器:
import Ember from 'ember';
export default Ember.Component.extend({
copied:false,
actions:{
deleteAsset(asset){
this.attrs.deleteAsset(asset);
},
successfulCopy(btn){
console.log(this.$(btn));
this.$(btn).toggleProperty('copied', true);
Ember.run.later(()=>{
this.$(btn).toggleProperty('copied', false);
},500);
}
}
});
当我单击带有文本Copy Link
的按钮时,组件会按原样切换复制的属性,但是,它会切换列表中所有项目的属性,从而更改其所有文本。在操作successfulCopy
中,我引用了单击按钮的HTML。如何仅将那个组件的复制属性切换为仅切换该按钮的文本?
答案 0 :(得分:0)
主要组分,
{{#copy-button
clipboardText=item.url
class="btn"
success="successfulCopy" as |copied|
}}
{{fa-icon "chain" title="Copy to Clipboard"}} {{unless copied "Copy Link" "Copied"}}
{{/copy-button}}
复制-button.hbs 强>
copied
组件中提供了copy-button
属性,因此要访问它main-component
,它应该会产生它。
{{yield copied}}
复制-button.js 强>
successfulCopy
函数将切换自己的属性copied
。所以你不需要传递参数而你不需要jquery的东西,因为你已经编写了基于逻辑的copied
属性。只需切换copied
即可完成其余工作。
import Ember from 'ember';
export default Ember.Component.extend({
init(){
this._super(...arguments);
this.set('copied',false);
}
actions:{
deleteAsset(asset){
this.get('deleteAsset')(asset);
},
successfulCopy(){
this.toggleProperty('copied');
}
}
});
答案 1 :(得分:0)
试试这个:
successfulCopy(btn){
this.set('item.copied', true)
}
{{fa-icon "chain" title="Copy to Clipboard"}} {{unless item.copied "Copy Link" "Copied"}}