使用underscore.js加载模板时再次调用document.ready

时间:2014-08-13 09:36:19

标签: javascript jquery node.js underscore.js

我只是尝试使用underscore.js加载模板,但是当我点击登录按钮并向服务器发出请求然后在我尝试删除一个模板并显示其他模板后检索响应后,在该文档之后再次调用.ready()导致重新呈现登录页面。

我很困惑导致这种情况发生的原因,任何帮助都表示赞赏。

这是javscript代码:

$(document).ready(function(){
    /* Login-Workflow-start*/
        var loginTemplate = _.template($("#login-template").html());
        $("#main-content").html(loginTemplate);

        $("#btn_login").on('click', function(){
          // when login is clicked.
          var formData = $("#form_login").serialize()
          console.log(formData);
          var req= $.ajax({
              type: "POST",
              url: "http://localhost:3002/login",
              data: {
                "username": $('[name="username"]').val(),
                "password": $('[name="password"]').val()
              },
              success: function(responseData,textStatus,jqXhr){
                console.log("Response: " + responseData);
                // show welcome page now
                var welcomeTemplate = _.template($("#welcome-template").html());
                $("#main-content").html(welcomeTemplate);   
              },
              error: function(){
                console.log("Error!!!");
              }
            });
        });
    /* Login-Workflow-end*/
});

更新

welcome-template如下所示:

<script type="text/template" id="welcome-template">
  <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Project name</a>
        </div>
        <div class="navbar-collapse collapse">
          <ul class="nav navbar-nav navbar-right">
            <li><a href="#">Dashboard</a></li>
            <li><a href="#">Settings</a></li>
            <li><a href="#">Profile</a></li>
            <li><a href="#">Help</a></li>
          </ul>
          <form class="navbar-form navbar-right">
            <input type="text" class="form-control" placeholder="Search...">
          </form>
        </div>
      </div>
    </div>
    <div class="container-fluid">
      <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
          <ul class="nav nav-sidebar">
            <li class="active"><a href="#">Overview</a></li>
            <li><a href="#">Reports</a></li>
            <li><a href="#">Analytics</a></li>
            <li><a href="#">Export</a></li>
          </ul>
          <ul class="nav nav-sidebar">
            <li><a href="">Nav item</a></li>
            <li><a href="">Nav item again</a></li>
            <li><a href="">One more nav</a></li>
            <li><a href="">Another nav item</a></li>
            <li><a href="">More navigation</a></li>
          </ul>
          <ul class="nav nav-sidebar">
            <li><a href="">Nav item again</a></li>
            <li><a href="">One more nav</a></li>
            <li><a href="">Another nav item</a></li>
          </ul>
        </div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
          <h1 class="page-header">Dashboard</h1>      
        </div>
      </div>
    </div>
  </script>

登录模板如下所示:

<script type="text/template" id="login-template">
        <ul class="nav nav-tabs" role="tablist">
          <li class="active"><a href="#login" role="tab" data-toggle="tab">Login</a></li>
          <li><a href="#register" role="tab" data-toggle="tab">Register</a></li>
          <li><a href="#reset-password" role="tab" data-toggle="tab">Reset Password</a></li>
        </ul>

        <!-- Tab panes -->
        <div class="tab-content">
          <div class="tab-pane active" id="login">
              <form role="form" id="form_login">
                <div class="form-group">
                  <div class="input-group">
                    <span class="input-group-addon">Username</span>
                    <input type="text" class="form-control" name="username" placeholder="Enter Username here" />
                  </div>
                </div>
                <div class="form-group">
                  <div class="input-group">
                    <span class="input-group-addon">Password</span>
                    <input type="text" class="form-control" name="password" placeholder="Enter Password here" />
                  </div>
                </div>
                <button class="btn btn-wide btn-primary mrm" id="btn_login">Login</button>
              </form>
          </div>
          <div class="tab-pane" id="register"> Register page loading..</div>
          <div class="tab-pane" id="reset-password">Reset Password page Loading</div>
        </div>
    </div>
</script>

UPDATE2 如果有人想要查看整个/部分代码进行分析,请访问此repo on bitbucket

1 个答案:

答案 0 :(得分:0)

得到解决方案,

Culprit是表单标签,单击提交按钮后,表单标签也发出了自己的请求,这导致document.ready被重新调用。

解决方案:将form代码转换为div,因为我们正在处理自己创建的请求。