2013-04-03 41 views
4

我使用Durandal模板開發了一個asp.net解決方案。從Durandal模式對話框返回一些東西

我想使用模式對話框選擇表中的一個元素並返回選擇元素的主視圖模型。

這是我到目前爲止有:

在我有一個鏈接(changer)讓我打開模式,對話的主要觀點:

enter image description here

這裏是我的視圖模型的功能點擊鏈接時稱爲:

var changeSender = function (item) { 
    app.showModal('viewmodels/sender'); 
}; 

於是我打開名爲sender對話框。

下面是sender的視圖模型:

define(function (require) { 

    var system = require('durandal/system'), 
     datacontext = require('services/datacontext'); 

    var senders = ko.observableArray(); 

    var activate = function() { 
     return datacontext.getSenders(senders); 
    }; 

    var buttonOk = function (dialogResult) { 
     this.modal.close(dialogResult); 
    } 

    var buttonCancel = function() { 
     this.modal.close(); 
    } 

    var vm = { 
     activate: activate, 
     senders: senders, 
     buttonOk: buttonOk, 
     buttonCancel: buttonCancel 
    }; 

    return vm; 
}); 

下面是'sender'的觀點:

<div class="messageBox autoclose" style="max-width: 500px"> 
    <div class="modal-header"> 
     <h3>Expéditeur</h3> 
    </div> 
    <div class="modal-body"> 
      <table class=""> 
      <thead> 
       <tr> 
        <th></th> 
        <th>Name</th> 
        <th>Street</th> 
        <th>City</th> 
       </tr> 
      </thead> 

      <tbody data-bind="foreach: senders"> 
       <tr data-bind="attr: {'data_id':id}"> 
        <td><input type="radio" name="isSelected" data-bind="checked: isSelected" /></td> 
        <td data-bind="text: name"></td> 
        <td data-bind="text: street"></td> 
        <td data-bind="text: city"></td> 
       </tr> 
      </tbody> 
     </table> 
    </div> 
    <div class="modal-footer"> 
     <button class="btn btn-primary" data-bind="click: buttonOk">Ok</button> 
     <button class="btn" data-bind="click: buttonCancel">Cancel</button> 
    </div> 
</div> 

enter image description here

我的問題是我不知道如何識別所選的單選按鈕並將其返回到主視圖。

任何幫助greathly讚賞。

謝謝。

+0

有關你們快速的問題。在你的按鈕中,你如何/爲什麼要訪問這個.modal? –

+0

看看這個:http://durandaljs.com/documentation/Showing-Message-Boxes-And-Modals/有'dialog.close(...)'但在我的代碼中我使用'modal.close' 。我仍然在使用Durandal 1.2,或許他們在新的2.0版本中將'模態'改爲'對話'?我不知道。另一篇文章:http://stackoverflow.com/questions/18245984/programmatically-closing-a-durandal-modal希望它有幫助。 – Bronzato

+0

被添加到vm的對象實際上是2.0中的__dialog__。所以我使用這個.__對話框__。close();'關閉模式 – Justin

回答

7

你可以在dialogResult對象上添加任何你想要的東西或完全替換它。

var buttonOk = function (dialogResult) { 
    dialogResult.checkedValue = 'Cogema'; 
    this.modal.close(dialogResult); 
} 

然後,您可以從該對話框訪問結果在主視圖模型是這樣的:

var changeSender = function (item) { 
    app.showModal('viewmodels/sender').then(function(dialogResult){ 
     // use dialogResult.checkedValue here 
    }); 
}; 
相關問題