2014-09-10 31 views
1

我已經搜索,並找到了舊的答案,並且手動清除bidnigns,然後調用applybindings ...以及創建一個自定義視圖模型,它不直接關係到anythign其他和在出現新的DOM節點後應用綁定。Knockout 3.2 applyBindings到新的DOM節點

我的具體情況是,bootstrap3模態基本上在調用show之後重新附加自己,同樣,模態中的所有項目實際上仍然沒有綁定。

我已經有一個模型,表示對話框的狀態/屬性,但我想dialogviewmodel是我的pageViewModel的孩子。

我的問題是,在這個時候,什麼是最合適的方式來做到這一點?有沒有辦法說這個節點,附加到視圖模型在這個屬性?

回答

0

我正在使用與您相同的工具(敲除和引導程序3)。我這是怎麼做到情態動詞爲例:

這個div存在的身體在我的index.html的第一個元素:

<div class="modal fade" id="BaseModal" tabindex="-1" role="dialog" aria-labelledby="BaseModal" aria-hidden="true" data-backdrop="static" data-keyboard="false"> 
    <div id="modalLoading" class="ajax-loader"> 
     <img src="images/working_blue.gif" /> 
    </div> 
    <div class="modal-dialog modal-lg"> 
     <div id="ModalAnchor" class="modal-content col-lg-4 col-lg-offset-4" data-bind="html: $data.ModalContent"></div> 
    </div> 
</div> 

// This is typescript but the javascript should be pretty close to this. 
private LoadModalView(viewName: string, viewModel: any): void { 
    $.get(viewName) 
     .then((view: any, status: string, jxhr: JQueryXHR) => { 
      this.ModalContent(''); // ensure there is no content in the modal. 
      this.ModalContent(view); // Load new HTML 
      ko.applyBindingsToDescendants(viewModel, this.ModalAnchor[0]); // bind new modal viewmodel 
      this.BaseModal.modal('show'); // Open the modal 
     }, (view: any, status: string, jxhr: JQueryXHR) => { /*Do error handling*/ }); 
} 

編輯:這是我裝入的HTML的例子模態:

<div class="modal-header"> 
    <h3 data-bind="text: $data.Header"></h3> 
</div> 
<div class="modal-body"> 
    <h5 data-bind="text: $data.Message"></h5> 
</div> 
<div class="modal-footer"> 
    <button id="OK" class="btn btn-primary center-block" data-bind="click: OK, text: ButtonText" /> 
</div> 

在LoadModalView功能,視圖模型參數是代表新的模態的新數據/內容/等一個視圖模型。

在模態Viewmodel中,我在模態中調用'hide'時會提供目的。

我希望這有助於!我不知道這是否是「正確」或「正確」的方式來做到這一點,但這是我的模式。

+1

我實際上已經想出了這一點,在更多的方式...但這也可以...並應被視爲答案。鑑於這必須是一個非常普遍的現象,我認爲淘汰賽應該記錄這個或者他們推薦的任何方法。 – Ronnyek 2014-09-11 16:48:05