从长远来看,最好的选择可能是将这些引导小部件转换为淘汰自定义绑定(例如https://github.com/billpull/knockout-bootstrap),甚至更好地转换为原生 Durandal 小部件。
查看 Durandal 1.2 messageBox 是如何实现的。这可以用作将引导选项卡和下拉菜单转换为 Durandal 小部件的粗略指南。如您所见href
,视图中的属性已替换为data-bind="click: ..."
. 此外,您必须将 Bootstrap JavaScript 转换为 Durandal viewmodel
。
例如http://twitter.github.io/bootstrap/javascript.html#modals
<div class="modal hide fade">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3>Modal header</h3>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<a href="#" class="btn">Close</a>
<a href="#" class="btn btn-primary">Save changes</a>
</div>
</div>
变为
https://github.com/dFiddle/dFiddle-1.2/blob/gh-pages/App/durandal/messageBox.html
<div class="messageBox">
<div class="modal-header">
<h3 data-bind="html: title"></h3>
</div>
<div class="modal-body">
<p class="message" data-bind="html: message"></p>
</div>
<div class="modal-footer" data-bind="foreach: options">
<button class="btn" data-bind="click: function () { $parent.selectOption($data); }, html: $data, css: { 'btn-primary': $index() == 0, autofocus: $index() == 0 }"></button>
</div>
</div>
更新:这是一个简约的选项卡小部件实现。现场版http://dfiddle.github.io/dFiddle-2.0/#bootstrap
看法:
<div class="tabs">
<ul class="nav nav-tabs" data-bind="foreach: { data: settings.items }">
<li data-bind="css: {active: isActive}">
<a data-bind="text: name, click: $parent.toggle.bind($parent)"></a>
</li>
</ul>
<div class="tab-content" data-bind="foreach: { data: settings.items}">
<div class="tab-pane" data-bind="html: content, css: {active: isActive}"></div>
</div>
</div>
视图模型:
define(['durandal/composition', 'jquery'], function(composition, $) {
var ctor = function() { };
ctor.prototype.activate = function(settings) {
this.settings = settings;
};
ctor.prototype.detached = function() {
console.log('bootstrap/widget/viewmodel: detached', arguments, this);
};
ctor.prototype.toggle = function(model, event){
this.deactivateAll();
model.isActive(true);
};
ctor.prototype.deactivateAll = function(){
$.each(this.settings.items(), function(idx, tab){
tab.isActive(false);
});
};
return ctor;
});