4

我正在尝试在 durandal 2.0 应用程序中连接一些引导下拉导航:

<ul class="nav nav-pills">
    <li class="dropdown">
        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
            <span>Dropdown</span>
            <b class="caret"></b>
        </a>
        <ul class="dropdown-menu">
            <li><a tabindex="-1" href="#/view/a>Option A</a></li>
            <li><a tabindex="-1" href="#/view/b">Option B</a></li>
        </ul>
    </li>
</ul>

单击“下拉菜单”会导致 Durandal 的路由器尝试导航到根路由(即“#”或“”)。并且从不打开菜单。这个对吗?解决方法?

更新

标签也有同样的问题:

<div class="tabbable">
    <ul class="nav nav-tabs">
        <li class="active"><a href="#tab1" data-toggle="tab">Section 1</a></li>
        <li><a href="#tab2" data-toggle="tab">Section 2</a></li>
    </ul>
    <div class="tab-content">
        <div class="tab-pane active" id="tab1">
        <p>I'm in Section 1.</p>
    </div>
    <div class="tab-pane" id="tab2">
        <p>Howdy, I'm in Section 2.</p>
        </div>
    </div>
</div>
4

3 回答 3

6

这个问题其实很简单。与路由模块没有冲突。我假设启动工具包中默认包含引导 javascript,但事实并非如此。

修复就像在我的 require 依赖项中添加额外的行一样简单:

var system = require('durandal/system'),
    app = require('durandal/app'),
    viewLocator = require('durandal/viewLocator');
require('bootstrap');

请注意,我的 require.config 是:

require.config({
    paths: {
        'text': '../lib/require/text',
        'durandal':'../lib/durandal/js',
        'plugins' : '../lib/durandal/js/plugins',
        'transitions' : '../lib/durandal/js/transitions',
        'knockout': '../lib/knockout/knockout-2.3.0',
        'bootstrap': '../lib/bootstrap/js/bootstrap',
        'jquery': '../lib/jquery/jquery-1.9.1'
    },
    shim: {
        'bootstrap': {
            deps: ['jquery'],
            exports: 'jQuery'
       }
    }
});
于 2013-07-25T14:57:09.380 回答
2

从长远来看,最好的选择可能是将这些引导小部件转换为淘汰自定义绑定(例如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">&times;</button>
    <h3>Modal header</h3>
  </div>
  <div class="modal-body">
    <p>One fine body…&lt;/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;
});
于 2013-07-25T07:36:57.883 回答
0

您是否尝试过将包含元素更改为 div 或 span 而不是锚标记?

于 2013-07-25T07:31:05.207 回答