1

我有一个包含 3 个页面的应用程序,我试图在点击时呈现。第一页是登陆页面。其他两个应在单击链接时呈现。它们都包含在同一个容器 div#modal-content 中

我的HTML如下:

<script type="text/x-handlebars-template" id="landingPage">
    <div>
       <div class="auth-wrapper">
           <div class="logo">
               <img src="img/login/logo-landing.png"/>
           </div>
           <div class="to-auth-buttons-wrapper">
               <a class="btn-to-auth btn-login" href="#signup-page">Sign Up</a>
               <a class="btn-to-auth btn-signup" href="#login-page">Log In</a>
           </div>
       </div>
    </div>
</script>

我的路由器功能如下:

var Approuter = Backbone.Router.extend({
    initialize: function() {
        console.log('router initialized'); 
            Backbone.history.start({ pushState: true });   
    },

    routes: {
        '': 'main',
        'signup-page' : 'signup',
        'login-page' : 'login'
    },

    main: function() {
        this.landing = new LandingView({ el: $('#modal-content') });
        slider.slidePage(this.landing.$el);
    },

    signup: function() {
        this.signuppage = new SignUpView({ el: $('#modal-content') });
        console.log("LANDING VIEW: Signup clicked");
    },

    login: function() {
        this.loginpage = new LoginView({ el: $('#modal-content') });
        console.log("LANDING VIEW: Login clicked");
    }
});

并且视图文件如下:

var SignUpView = Backbone.View.extend({
    initialize: function() {
        this.render();
    },

    render: function() {
        var template = Handlebars.compile($('#signUpPage').html());

        this.$el.html(template);
    },
});

var LoginView = Backbone.View.extend({
    initialize: function() {
        this.render();
    },

    render: function() {
        var template = Handlebars.compile($('#loginPage').html());

        this.$el.html(template);
    },
});

此外,这是我的模板:

<div id="modal-content">

  <script type="text/x-handlebars-template" id="landingPage">
      <div>
         <div class="auth-wrapper">
             <div class="logo">
                 <img src="img/login/logo-landing.png"/>
             </div>
             <div class="to-auth-buttons-wrapper">
                 <a class="btn-to-auth btn-login" href="#/signup-page">Sign Up</a>
                 <a class="btn-to-auth btn-signup" href="#/login-page">Log In</a>
             </div>
         </div>
      </div>
  </script>

  <script type="text/x-handlebars-template" id="signUpPage">
      <div>

         <div class="header">
             <a href="#" class="btn">Back</a>
         </div>
         <div class="auth-wrapper">


             <div class="logo">
                 <img src="img/login/logo-landing.png"/>
             </div>

             <form method="post" id="userSignUp">       
                 <input class="form-control input-signin" type="text" name="username" placeholder="Name" id="userName">
                 <input class="form-control input-signin" type="text" name="useremail" placeholder="Email" id="userEmail"> 
                 <input class="form-control input-signin" type="text" name="userpass" placeholder="Password" id="userPassword">

                 <a class="btn-to-auth btn-login js-btn-login">Sign Up</a>
             </form>
         </div>

      </div>
  </script>

  <script type="text/x-handlebars-template" id="loginPage">
      <div>
         <div class="header">
             <a href="#" class="btn">Back</a>
         </div>
         <div class="auth-wrapper">


             <div class="logo">
                 <img src="img/login/logo-landing.png"/>
             </div>

             <form method="post" id="userSignIn">       
                 <input class="form-control input-signin" type="text" name="useremail" placeholder="Email" id="userEmail"> 
                 <input class="form-control input-signin" type="password" name="userpass" placeholder="Password" id="userPassword">
                 <a class="btn-to-auth btn-login js-btn-login">Log In</a>
             </form>
         </div>

      </div>
  </script>

 </div>

我的问题 单击 a#signup-page 或 a#login-page 链接后,我可以看到 url 更改为“localhost/#signup-page”,但视图未呈现。

当我在 localhost/#signup-page 或 localhost/#login-page 刷新页面时,我看到视图已呈现。

我哪里错了?

4

1 回答 1

1

Please take a look at the code above:

<html>

    <body>
        <div class="action">
            <a name="routeOne" href="#routeTwo">routeOne</a>
            <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
            <a name="routeTwo" href="#routeOne">routeTwo</a>
        </div>
        <div class="output"></div>


        <script type="text/javascript"  src="lib/jquery.js"></script>
        <script type="text/javascript"  src="lib/underscore.js"></script>
        <script type="text/javascript"  src="lib/backbone.js"></script>

        <script type="text/javascript">
            var Approuter = Backbone.Router.extend({
                initialize: function() {
                    console.log('router initialized'); 
                    Backbone.history.start({pushState:true});   
                },

                routes: {
                    '': 'main',
                    'routeOne' : 'routeOne',
                    'routeTwo' : 'routeTwo'
                },

                main: function() {
                    console.log("main");
                },

                routeOne: function() {
                    console.log("routeOne");
                },

                routeTwo: function() {
                    console.log("routeTwo");
                }
            });

            var routes = new Approuter();

        </script>
    </body>
</html>

Edit1: Differences between Routes and pushState

Backbone Routes enable you to monitoring hasChange history events (changes in url) and trigger some js code when found some change at url (http://localhost/backbone-test/#someRoute), That's amazing because we can trigger some complex actions done by user at your web site, just calling an url.

pushState enable you to hide this '#' hash and turn your url more readable, but as the backbone documentation said

"if you have a route of /documents/100, your web server must be able to serve that page, if the browser visits that URL directly."

Then, if you use pushState:true your url become more readable, http://localhost/backbone-test/#someRoute to http://localhost/backbone-test/someRoute but you need to create a back-end to answer directly access to you readable url.

When pushState is true and you call href="#someRoute" the browser understand this as a html anchor.

于 2013-09-24T00:47:16.860 回答