2016-06-27 61 views

回答

4

是的,有。

Aurelia-dialog提供了一個抽象的Renderer接口,它用來與渲染器接口。默認情況下,它會使用它提供的渲染器,但你可以通過配置對話框插件覆蓋此,像這樣:

import {MyRenderer} from './my-renderer'; 

aurelia.use.plugin('aurelia-dialog', (config) => { 
    config.useRenderer(MyRenderer); 
}); 

...其中MyRenderer使用abstract Renderer interface。在渲染器中,您需要實現三種方法:getDialogContainer,showDialoghideDialog

有些注意事項 - 在您的showDialog函數中,您需要創建showDialoghideDialog方法並將它們附加到作爲參數傳遞的dialogController。這可以確保您的dialogController可以編程方式關閉對話框。

在實現並註冊了渲染器後,對話框插件將使用您選擇的UI工具包。希望這可以幫助。

+0

這是你添加到插件的部分之一嗎? –

+1

@MatthewJamesDavis Yup。事實上,這是一段時間 - 拉的請求可以在這裏找到(https://github.com/aurelia/dialog/pull/120)。 –

+0

您能演示如何在您的答案中實現渲染器嗎? –

1

這是我的解決方案(在TypeScript中)的語義UI模塊,它不使用aurelia對話框。

的視圖(/ui/dialogs/dialog-confirm.html):

<template> 
    <div class="ui modal small" ref="dialogElement"> 
     <div class="header">${model.headerText}</div> 
     <div class="content"> 
      <p>${model.messageText}</p> 
     </div> 
     <div class="actions"> 
      <div class="ui approve button">${model.confirmText?model.confirmText:'OK'}</div> 
      <div class="ui cancel button">${model.cancelText?model.cancelText:'Cancel'}</div> 
     </div> 
    </div> 
</template> 

視圖模型(/ui/dialogs/dialog-confirm.ts):

export class Dialog { 
    model; 
    done; 
    result; 
    dialogElement:HTMLElement; 
    activate(data) { 
     if(data){ 
      this.model = data.model; 
      this.done = data.done; 
      this.result = false; 
     } 
    } 
    bind(){ 
     $(this.dialogElement) 
      .modal({ 
       onApprove :()=>{ this.result = true; }, 
       onDeny :()=>{ this.result = false; }, 
       onHide :()=>{ this.done(this.result); } 
      }) 
      .modal('show'); 
    } 
} 

對話框類(/ui/dialogs/dialog.ts):

import { inject } from 'aurelia-framework'; 
import { EventAggregator } from 'aurelia-event-aggregator'; 

@inject(EventAggregator) 
export class Dialog { 
    constructor(private eventAggregator) { 
    } 
    show(viewName: string, model) { 
     return new Promise((resolve, reject) => { 
      this.eventAggregator.publish('showDialog', { 
       viewName: viewName, 
       model: model, 
       resolve: resolve 
      }); 
     }); 
    } 
} 

...注入EventAggregator到您的應用程序類,這增加了連接()掛鉤:

attached() { 
    this.eventAggregator.subscribe('showDialog', (event) => { 
     console.assert(!this.dialogData, "Only one dialog can be shown at any time."); 
     event.done = (result) => { 
      event.resolve(result); 
      this.dialogData = null; 
     } 
     this.dialogName = event.viewName; 
     this.dialogData = event; 
    }); 
} 

...終於添加到您的app.html:

<compose if.bind="dialogData" view-model="./ui/dialogs/${dialogName}" model.bind="dialogData" view="./ui/dialogs/${dialogName}.html"> 
</compose> 

用法,你可以給任何模型 - 視圖/視圖對作爲第一個參數的名稱:

this.dialog.show('dialog-confirm',{ 
    headerText:'Warning!', 
    messageText:'When you delete stuff - it is lost', 
    confirmText:'Delete', 
    cancelText:'I better not...' 
}).then(function(result){ 
    console.log('The result is: '+result) 
}); 
+0

我們需要一個在babel中的解決方案!:) – Reft

+0

只需刪除類型(:HTMLElement)並在Dialog中更新eventAggregator注入,它應該可以工作 –

相關問題