我能够解决这个问题,虽然我不确定我的解决方案是不是最优雅的,但它对我来说效果很好。
我做的第一件事是将 BOOTSTRAP 模态的 html 放在 items.html(呈现项目的视图模型的视图)上,如下所示:
<div id="itemDetails" class="modal hide fade" tabindex="-1" role="dialog" aria-abelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Modal header</h3>
</div>
<div class="modal-body" data-bind="compose: { model: 'viewmodels/itemdetail', activate: activateItemDetail }" >
<!-- -->
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button class="btn btn-primary">Save changes</button>
</div>
请注意,我将绑定线放在 modal-body 内
data-bind="compose: { model: 'viewmodels/itemdetail', activate: activateItemDetail }"
并注意我将属性 activateItemDetail 分配给“激活”。当用户单击调用 modal 的 dom 元素时,activateItemDetail 被分配为“true”。activateItemDetail 订阅了名为 itemId 的 Items 视图模型的另一个属性,如下所示:
vm.itemId.subscribe(function(){
this.activateItemDetail(true);
}, vm);
所以现在我们知道只有在我们收到用户的 id 后才会激活详细视图模型。
接下来,在 itemdetail 视图模型激活中,我放置了以下代码
function activate(){
//get the item id from the items - you need to require the items model
var id = items.requestedItemId;
//now you need to get the data from your service
dataService.getItemDetail(item, itemId),
//now you call the modal
$('#itemdetail').modal.show();
}
您需要做的最后一件事是在完成后停用 itemdetail 视图模型。您可以通过数据绑定停用功能到 Bootstrap 模式的“隐藏”事件来做到这一点,如下所示:
data-bind="event: {hidden: closeItemDetails}"
然后您只需像这样停用:
this.activateItemDetail(false);
就是这样。