简体   繁体   English

如何使用敲除和durandal对点击功能进行数据绑定

[英]How to data-bind on click function using knockout and durandal

I have a topbar in my application which is being used in several html files. 我的应用程序中有一个顶栏,该顶栏正在多个html文件中使用。 I made a html template of that topbar and I am including that html file in all the other html files where I need that topbar. 我制作了该topbar的html模板,并将该html文件包括在需要该topbar的所有其他html文件中。

main.html main.html

<style type = "text/css">

.glyphicon.glyphicon-user
{
font-size: 38px;
}

.glyphicon.glyphicon-refresh
{
font-size: 18px;
}
</style>

<div id="myElement">

<div id="includedContent"></div> // this is where included html file is rendered

 //here I have other contents 

</div>

main.js main.js

$("#includedContent").load("./app/views/topbar.html"); // This is how I call that html file

Now, I get the html file and the topbar is rendered on the screen. 现在,我得到了html文件,并且顶部栏显示在屏幕上。 In that top bar, I have a dropdown which has a modal dialog that gets called using data-bind : click . 在该顶部栏中,我有一个下拉列表,其中包含一个使用数据绑定调用的模式对话框 :click Other links are working fine but dialog box doesn't even get called. 其他链接工作正常,但对话框甚至没有被调用。 I am not sure what the problem is, maybe I should not call html inside another html like this in durandal . 我不确定是什么问题,也许我不应该在durandal这样的另一个html内部调用html。

This is my code for topbar 这是我的topbar代码

topbar.html topbar.html

   <div class="container">
     <div class="navbar navbar-default blue-background">

    <div class="navbar-header pull-right">



        <ul class="nav navbar-nav" style="display:inline-block;">
            <div class="col-lg-4 col-md-4 col-sm-2 col-xs-2 ">

                <div class="col-lg-3 col-md-3 col-sm-1 col-xs-1">
                    <li><a href="#sync"><span class="glyphicon glyphicon-refresh" style="color:white"></span></a></li>
                </div>

                <div style = "margin-right: 20px">
                    <li>
                        <div class="dropdown">
                            <span data-toggle="dropbtn" class="glyphicon glyphicon-user" style="color:white"></span>
                            <div class="dropdown-content">
                                <a  data-bind="click:ChangePassword" style="font-size: 14px; color:blue;" ><label>Change Password</label></a>
                                <a id="Logout" href="#" style="font-size: 14px; color:blue"><label>Logout</label></a>
                            </div>

                        </div>
                    </li>
                </div>

            </div>

        </ul>


    </div>



      </div>

   </div>

topbar.js topbar.js

  define(['plugins/router', 'durandal/app', 'knockout','./changepassword'], function(router, app, ko,changepassword) {



return
{

    ChangePassword: function()
    {
      alert("!!!!!!");
       changepassword.show();

    }

 };
});

So, I get the dropdown but I don't get the dialog box when I click on change password. 所以,我得到了下拉菜单,但是当我单击更改密码时却没有得到对话框。 I hope I am able to explain my problem. 我希望我能解释我的问题。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

This isn't working because you're using jQuery to load the topbar template into the correct space on each page, rather than using Durandal's composition engine to do it for you. 这是行不通的,因为您正在使用jQuery将topbar模板加载到每个页面上的正确空间中,而不是使用Durandal的撰写引擎来为您完成此操作。 It's the Durandal composition engine that binds the view to the view model. 将视图绑定到视图模型的是Durandal合成引擎。

What you should typically be doing if using Durandal, is create a shell page for your application , and the topbar resides there, bound using Durandal, thus allowing you to use the Knockout click binding . 如果使用Durandal,通常应该做的是为您的应用程序创建一个shell页面 ,并且顶部栏位于此处,并使用Durandal进行绑定,从而允许您使用Knockout click绑定 Perhaps using the compose binding. 也许使用compose绑定。 The shell page then has an area defined to host the "pages" that make up the rest of your application. 然后,shell页面具有一个定义区域,用于托管构成应用程序其余部分的“页面”。 Typically you'd use the Router for this. 通常,您将为此使用路由器

I'm using Durandal and the only place I use jQuery in my entire application is in a couple of custom Knockout bindings . 我正在使用Durandal,在整个应用程序中唯一使用jQuery的地方是几个自定义的Knockout绑定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM