Dialog 매번 만들고 있다면? — Fragment 재사용 패턴 #shorts #SAP #UI5
Moderator
· 조회 3
이 글이 답하는 질문
- XML Fragment로 재사용 가능한 Dialog를 어떻게 만드나요?
- Dialog를 매번 새로 생성하면 안 되는 이유는?
직접 해보기
1. ConfirmDialog.fragment.xml
<core:FragmentDefinition xmlns="sap.m" xmlns:core="sap.ui.core">
<Dialog title="확인" type="Message">
<content><Text text="계속하시겠습니까?" /></content>
<buttons>
<Button text="확인" press=".onConfirm" type="Emphasized" />
<Button text="취소" press=".onCancel" />
</buttons>
</Dialog>
</core:FragmentDefinition>
2. Controller — lazy init 패턴
onOpenDialog: function () {
if (!this._pDialog) {
this._pDialog = Fragment.load({
id: this.getView().getId(),
name: "com.app.fragment.ConfirmDialog",
controller: this
}).then(oDialog => {
this.getView().addDependent(oDialog);
return oDialog;
});
}
this._pDialog.then(oDialog => oDialog.open());
}
삽질 노트
addDependent()누락 → View 모델 상속 불가, 바인딩 값이 비어 보입니다.Fragment.load()는 Promise →.then()없이 쓰면 Dialog가 안 열립니다.- View ID를 prefix로 지정해야 ID 충돌이 없습니다.
핵심 한 줄
Fragment.load()로 한 번만 생성하고 addDependent()로 View에 붙이면, Dialog는 open()/close()만으로 재사용됩니다.