【问题标题】:Autocomplete Not Working in DurandalJS Modal View自动完成在 DurandalJS 模态视图中不起作用
【发布时间】:2014-01-25 18:08:49
【问题描述】:

我正在为我的 PHP Web 应用程序使用 DurandalJS 框架。我正在利用 DurandalJS 框架功能来显示模态视图。

我有一个主页 home.html,其中包含一个指向名为 autocomplete.html 的页面的链接。单击此链接时,它将在模式视图中打开 autocomplete.html 页面(DurandalJS 提供的功能)。

我还使用 jQuery-UI 自动完成功能为文本框创建自动完成功能。当用户在文本框中输入任何内容时,他会根据他通过键盘输入的字符获得一个建议列表。

这里的问题是,如果 autocomplete.html 页面在浏览器中独立运行,则自动完成功能会起作用。但是,当页面以模式显示时,即通过 DurandalJS 框架运行(导航)项目时,此功能不会运行。

谁能告诉我我到底哪里出错了?尽早回复将不胜感激。

我的项目的源代码如下。请注意,我提供源代码的顺序与执行 DurandalJS 导航调用堆栈的顺序相同。我的应用程序的流程是,index.php >>> main.js >>> shell.js >>> shell.html >>> home.js >>> home.html >>> autocomplete.js >>> autocomplete .html。

autocomplete.js >>> autocomplete.html 调用堆栈在用户单击 home.html 页面上的 Go to Autocomplete 链接时执行。

main.js

require.config({
    paths: {
        'text': 'durandal/amd/text'
    }
});

define(function (require) {
    var app = require('durandal/app'),
        viewLocator = require('durandal/viewLocator'),
        system = require('durandal/system'),
        router = require('durandal/plugins/router');

    //>>excludeStart("build", true);
    system.debug(true);
    //>>excludeEnd("build");

    app.start().then(function () {
        //The following statement is to help DurandalJS to find files only according to their names.
        //Replace 'viewmodels' in the moduleId with 'views' to locate the view.
        //Look for partial views in a 'views' folder in the root.
        viewLocator.useConvention();

        //configure routing
        router.useConvention();
    router.mapNav("pages/home");
    router.mapNav("pages/autocomplete");

        app.adaptToDevice();

        //Show the app by setting the root view model for our application with a transition.
        app.setRoot('viewmodels/shell', 'entrance');
    });
});

shell.js

define(function (require) {
    var router = require('durandal/plugins/router');

    return {
        router: router,
        activate: function () {

        return router.activate('pages/home');
        }
    };
});

shell.html

<br />
<br />
<br />
<br />
<div class="container-fluid page-host">
<!--ko compose: { 
        model: router.activeItem, //wiring the router
        afterCompose: router.afterCompose, //wiring the router
        transition:'entrance', //use the 'entrance' transition when switching views
        cacheViews:true //telling composition to keep views in the dom, and reuse them (only a good idea with singleton view models)
    }--><!--/ko-->
</div>

home.js

    // JavaScript Document
    //This file loads the respective page's ViewModel (<Page>.js) and displays the View (<page>.html)

    define(function (require) {
        self.app = require('durandal/app');

        return {
            movies: ko.observable(),

            activate: function() {
                var self = this;

                //The following code in the function creates a modal view for the autocomplete page
                self.viewAutoCompleteModal = function(AutoComplete, element) {
                    app.showModal("viewmodels/pages/autocomplete");
                };
            }
        };
    });

home.html

<div id="applicationHost">
    <div class="navbar navbar-fixed-top navbar-inverse">
        <div class="navbar-inner">
            <div class="container">
                <a class="brand">
                    <span>My application</span>
                </a>
            </div>
        </div>
    </div>
</div>

<!--The following lines of code create href links for the My Application pages and directs the DurandalJS to the respective pages. The data-bind attribute calls the view<Page>Modal functions (which create a Modal view) which is defined in the ViewModel (<Page>.js file)-->
<br />
<br />
<a href="#/pages/autocomplete" data-bind="click: viewAutoCompleteModal">Go to Autocomplete</a>

autocomplete.js

// JavaScript Document
define(function (require) {
    var router = require('durandal/plugins/router');
    var moviesRepository = require("repositories/moviesRepository");

    return {
        activate: function (context) {

        }
    };

});

autocomplete.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8">
        <title>jQuery-UI Autocomplete Demo</title>
        <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
        <script src="http://localhost/rockontechnologies/Scripts/Script1.10.3/jquery-1.9.1.js"></script>
        <script src="http://localhost/rockontechnologies/Scripts/Script1.10.3/jquery-ui.js"></script>
        <link rel="stylesheet" href="/resources/demos/style.css">
        <script>
            $(function() {
                var availableTags = [
                    "ActionScript",
                    "AppleScript",
                    "Asp",
                    "BASIC",
                    "C",
                    "C++",
                    "Clojure",
                    "COBOL",
                    "ColdFusion",
                    "Erlang",
                    "Fortran",
                    "Groovy",
                    "Haskell",
                    "Java",
                    "JavaScript",
                    "Lisp",
                    "Perl",
                    "PHP",
                    "Python",
                    "Ruby",
                    "Scala",
                    "Scheme"
                ];
                $( "#tags" ).autocomplete({
                    source: availableTags
                });
            });
        </script>
    </head>

    <body>
    <div class="modal-footer">
    <ul class="btn-group">
        <button class="btn" data-bind="click: closeModal">Exit</button>
    </ul>
</div>
        <div class="ui-widget">
            <label for="tags">Tags: </label>
            <input id="tags">
        </div>
    </body>
</html>
  • 关于 DurandalJS 的帮助,我参考了: http://durandaljs.com/
  • 关于自动完成的帮助,我参考了:[http://jqueryui.com/autocomplete/][3]

提前谢谢你。

【问题讨论】:

    标签: autocomplete modal-dialog durandal hottowel


    【解决方案1】:

    我回答了一个类似的问题here,这将对您有所帮助。

    但是您的 autocomplete.html 是错误的,并且在由 Durandal 编写时将不起作用。您需要将其转换为 durandal 样式的 html 页面。

    将您的脚本标签添加到您的主机页面。在 Hot Towel 中,这是由包管理的,所以如果使用 PHP,我不完全确定在哪里添加这些。

    删除 HTML、SCRIPT、META 等...只留下纯 HTML 标记。

    例如:

        <div class="ui-widget">
            <label for="tags">Tags: </label>
            <input id="tags">
        </div>
    

    然后在您的 autocomplete.js 文件中,添加附加方法,或者如果使用 Durandal

        define(function (require) {
        var router = require('durandal/plugins/router');
        var moviesRepository = require("repositories/moviesRepository");
    
        return {
            activate: function (context) {
    
            },
            attached: function (view) {
    
                var $tagInput = $(view).find('#tags'); 
    
                 var availableTags = [
                        "ActionScript",
                        "AppleScript",
                        "Asp",
                        "BASIC",
                        "C",
                        "C++",
                        "Clojure",
                        "COBOL",
                        "ColdFusion",
                        "Erlang",
                        "Fortran",
                        "Groovy",
                        "Haskell",
                        "Java",
                        "JavaScript",
                        "Lisp",
                        "Perl",
                        "PHP",
                        "Python",
                        "Ruby",
                        "Scala",
                        "Scheme"
                    ];
    
                    $tagInput.autocomplete({
                        source: availableTags
                    });
    
    
            }
    
        };
    
    });
    

    如果您仍有问题并且不乐意提供帮助,请告诉我。

    【讨论】:

    • 非常感谢您的回复。我正在使用 Durandal 1.1.0,所以正如您所提到的,我将不得不尝试上述解决方案。正如您所说,我通过仅保留
      标记代码重组了我的 autocomplete.html 文件;但是,这样做之后,当我在浏览器中运行页面时,自动完成功能拒绝工作,而不再通过 Durandal 运行页面。因此,我带回了我的原始代码并开始逐个删除它。在消除所有标签后,自动完成功能仍然可以完美运行,除了所有
    • 请注意,当我说工作时,我的意思是只有页面在浏览器中独立运行,而不是通过 Durandal。在后一种情况下,即使所有代码都存在,自动完成功能也不起作用。 (2/2)
    • 好的,尝试将您的自动完成内容包装在另一个 div 中。 Durandal 将使用页面中的第一个 div 进行组合绑定,因此这很可能是原因。
    • 我也这样做了,但没有任何改变。目前我的 autocomplete.html 页面只有 3 个
    • 您肯定需要删除脚本标签并将其添加到您的 .php 页面。您的控制台窗口中是否还有任何错误?
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签