14

「OK」ボタンと「キャンセル」ボタンがコールバック関数をサポートする一般的なダイアログを追加したいと思います。

どうすれば Dojo AMD でこれを達成できますか?

例えば:

  myDialog = new Dialog ({

  title: "My Dialog",
  content: "Are you sure, you want to delete the selected Record?",
  style: "width: 300px",
  onExecute:function(){ //Callback function 
      console.log("Record Deleted") 
  },
  onCancel:function(){ 
      console.log("Event Cancelled") } 
  });
  // create a button to clear the cart
   new Button({ label:"Ok"
       onClick: myDialog.onExecute()
   }

   new Button({ label:"Cancel"
        onClick: function(){ myDialog.onCancel() }
   }
4

4 に答える 4

29

これが私が同じ質問に直面していたときに思いついた解決策です。完全にプログラム的ではありませんが、テンプレートを使用すると、コードが読みやすくなり、変更に対して柔軟になります。

100回聞くよりも、1回見る方がよいので、以下のすべてのコードをjsFiddleでライブで参照してください:http://jsfiddle.net/phusick/wkydY/

私が採用している主な原則はdijit.Dialog::content、文字列だけでなくウィジェットインスタンスでもあるという事実です。だから私はクラスdijit.Dialogを宣言するためにサブクラス化しConfirmDialogます。ConfirmDialog::constuctor()テンプレートからウィジェットを宣言してインスタンス化し、ダイアログのコンテンツになるように設定します。次に、メソッドonClickでアクションをワイヤリングします。ConfirmDialog::postCreate()

var ConfirmDialog = declare(Dialog, {

    title: "Confirm",
    message: "Are you sure?",

    constructor: function(/*Object*/ kwArgs) {
        lang.mixin(this, kwArgs);

        var message = this.message;

        var contentWidget = new (declare([_Widget, _TemplatedMixin, _WidgetsInTemplateMixin], {
            templateString: template, //get template via dojo loader or so
            message: message    
        }));
        contentWidget.startup();
        this.content = contentWidget;
    },

    postCreate: function() {
        this.inherited(arguments);
        this.connect(this.content.cancelButton, "onClick", "onCancel");
    }

})

テンプレートマークアップ:

<div style="width:300px;">

  <div class="dijitDialogPaneContentArea">
    <div data-dojo-attach-point="contentNode">
        ${message}              
    </div>
  </div>

  <div class="dijitDialogPaneActionBar">

    <button
      data-dojo-type="dijit.form.Button"
      data-dojo-attach-point="submitButton"
      type="submit"
    >
      OK
    </button>

    <button
      data-dojo-type="dijit.form.Button"
      data-dojo-attach-point="cancelButton"
    >
      Cancel
    </button>

  </div>

</div>

ConfirmDialog代わりに使用するdijit.Dialog

var confirmDialog = new ConfirmDialog({ message: "Your message..."});
confirmDialog.show();

重要:ダイアログコールバックへの接続を切断し、閉じたときにダイアログを破棄することを忘れないでください。

ConfirmDialogコードの複数の場所で頻繁に使用する場合は、次のことを考慮してください。

var MessageBox = {};
MessageBox.confirm = function(kwArgs) {
    var confirmDialog = new ConfirmDialog(kwArgs);
    confirmDialog.startup();

    var deferred = new Deferred();
    var signal, signals = [];

    var destroyDialog = function() {
        array.forEach(signals, function(signal) {
            signal.remove();
        });
        delete signals;
        confirmDialog.destroyRecursive();
    }

    signal = aspect.after(confirmDialog, "onExecute", function() {
        destroyDialog();
        deferred.resolve('MessageBox.OK');
    });
    signals.push(signal);

    signal = aspect.after(confirmDialog, "onCancel", function() {
        destroyDialog();   
        deferred.reject('MessageBox.Cancel');            
    });
    signals.push(signal);

    confirmDialog.show();
    return deferred;
}

コードが読みやすくなり、クリーンアップに対処する必要がなくなります。

MessageBox.confirm().then(function() {
    console.log("MessageBox.OK")
});
于 2012-05-01T23:52:31.600 に答える
7

Dojo 1.10 には、組み込みの [OK] ボタンと [Cancel] ボタンを備えた新しいdijit/ConfirmTooltipDialogが含まれています。

于 2015-01-17T00:18:28.620 に答える