【问题标题】:Render Backbone View on click (via Router push state)单击时渲染主干视图(通过路由器推送状态)
【发布时间】:2023-03-11 01:02:01
【问题描述】:

我有一个包含 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 刷新页面时,我看到视图已呈现。

我哪里错了?

【问题讨论】:

  • 尝试将pushState设置为false
  • 嗯..现在好像是下划线问题...

标签: javascript jquery backbone.js


【解决方案1】:

请看上面的代码:

<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:Routes 和 pushState 的区别

主干路由可以让你监控hasChange历史事件(url变化),当发现url变化时触发一些js代码(http://localhost/backbone-test/#someRoute),太神奇了,因为我们可以触发一些用户在您的网站上执行的复杂操作,只需调用一个 url。

pushState 使您能够隐藏此“#”哈希并使您的 url 更具可读性,但正如主干文档所说

"如果你有 /documents/100 的路由,你的 web 服务器必须能够 如果浏览器直接访问该 URL,则为该页面提供服务。”

然后,如果您使用pushState:true,您的 url 变得更具可读性,http://localhost/backbone-test/#someRoutehttp://localhost/backbone-test/someRoute 但您需要创建一个后端来回答直接访问您的可读 url。

当 pushState 为 true 并且您调用 href="#someRoute" 时,浏览器会将其理解为 html 锚点。

【讨论】:

  • pushstate: false 到底是做什么的?我的印象是 pushstate: true 对路由的发生至关重要
  • 谢谢!这很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多