我有一個模板內的組件。這個組件是一個彈出框,它在點擊按鈕時打開。這個彈出框中有複選框(全部默認設置爲false)。當我關閉此彈出框時,我想將所有變量完全重置爲默認設置,即所有複選框現在都已關閉。目前,當我重新打開這個彈出框時,前面的複選框仍然被選中。我該怎麼做,而無需手動切換每一個複選框:
this.set("checkbox1", false);
this.set("checkbox2", false);
so on...
是否有將自動重設組件,並取消所有的複選框,設置變量回false的功能?
相關代碼:
模板:應用程序/模板/ home.hbs
{{popup-modal isOpen=showModal}}
組件:應用程序/模板/組件/彈出窗口modal.hbs
{{#bs-modal body=false footer=false open=isOpen title="popup box" closeAction=(action "cancel") submitAction=(action "submit")}}
{{#bs-modal-body}}
<label><input type="checkbox" {{action "toggleCheckbox" "checkbox1" on="click" preventDefault=false}}/> Checkbox 1</label>
<label><input type="checkbox" {{action "toggleCheckbox" "checkbox2" on="click" preventDefault=false}}/> Checkbox 2</label>
{{/bs-modal-body}}
{{bs-modal-footer closeTitle="Cancel" submitTitle="Ok"}}
{{/bs-modal}}
組件JS:app/components/popup -modal.js
import Ember from 'ember';
export default Ember.Component.extend({
checkbox1: false,
checkbox2: false,
actions: {
submit(){
// close box
this.set('isOpen', false);
},
cancel(){
// how do I reset component here?
// in other words, make all checkbox set to false
// without manually doing it like below:
this.set("checkbox1", false);
this.set("checkbox2", false);
},
toggleCheckbox(checkbox){
this.toggleProperty(checkbox);
}
}
});
你可以分享component.js文件的代碼嗎? – Falke
有一個ember-twiddle.com複製,以查看組件如何工作,以及它在何處/如何使用它將是非常有益的。 – locks
我剛剛加了一段代碼 – IFH