【问题标题】:View inheritance doesn't render the both views视图继承不会呈现两个视图
【发布时间】:2012-09-30 16:20:30
【问题描述】:

我想在 Backbone.js 上构建我的第一个视图继承,非常经典,它是一个带有注册/登录/注销选项的导航栏模板,是我所有其他视图的父亲。

代码似乎可以找到,每个初始化方法都被调用,但它在渲染部分不起作用。

我可以实例化 templateFrontendView()(父亲),它工作得很好(我的视图上有导航栏),或者我可以实例化 homeView()(templateFrontendView 的子项),但我只会渲染这个,即使我在初始化方法中调用它,templateFrontendView 的渲染也会消失......

Here is the jsfiddle with all the code

     window.User = Backbone.Model.extend({
            defaults : {
                id : "1",
                username : "anonymous",
                facebook_id : "1235486421",
                facebook_token : "AEZH93FZFEFSFSFS4545154sfsfSF"
            },
            urlRoot: 'http://localhost:3000/user',
            initialize : function() {
                console.log('User Constructor...');                                                                                              
            this.url = "http://localhost:3000/user/"+this.get('username')+'/'+this.get('facebook_id')+'/'+this.get('facebook_token');
            }
  });


window.templateFrontendView = Backbone.View.extend({
        el : $('#template-frontend-container'),

        initialize : function() {
           this.model = new User();
           this.template = _.template($('#template-frontend-template').html());
           this.render();
        },

        events: {
          "click a.fb_connect": 'fetch_user'
        },

        fetch_user: function(){
            console.log("Save requested...");
        },render : function() {
            this.delegateEvents();
            var renderedContent = this.template(this.model.toJSON());
            $(this.el).html(renderedContent);
            return this;
        }
    });

    window.homeView = templateFrontendView.extend({
            el : $('#home-container'),

        initialize : function() {
           templateFrontendView.prototype.initialize.apply(this);
           this.model = new User();
           this.template = _.template($('#home-template').html());
           this.render();
        },

            render : function() {
                this.delegateEvents();
                var renderedContent = this.template(this.model.toJSON());
                $(this.el).html(renderedContent);
                return this;
            }
    });

    var App = {
        home: function() {
            var V = new homeView();
        }
    }

    $(document).ready(function(){
      App.home();
    });

还有风景

<!-- Template Frontend -->
      <script type="text/template" id="template-frontend-template">
        <div class="navbar">
          <div class="navbar-inner">
            <a class="brand" href="#">Chon the Road</a>
            <ul class="nav">
              <li class="active"><a href="#">Home</a></li>
              <li><a style="display: none;" class="account" href="#">Welcome <span><%= username %></span></a></li>
              <li><a style="display: none;" class="fb_connect" href="#"><img src="img/fb-connect.png" alt="fb-connect" /></a></li>
            </ul>
          </div>
        </div>

      </script>

      <div id='template-frontend-container'></div>
      <!-- Template Frontend -->

      <div id="fb-root"></div>

      <!-- Home -->
      <script type="text/template" id="home-template">
        <span><%= username %></span>
      </script>

      <div id='home-container'></div>
      <!-- Home -->

谢谢!

【问题讨论】:

  • 我会建议组合而不是继承在这里(以及几乎所有其他地方)。你没有链接render 调用,你会遇到this.template 的问题,你的$(this.el).html(...) 调用会互相争斗,...
  • 好的,我明白了,但是你建议我怎么做?因为我所有的视图都需要这个导航栏,视图继承的目标不是自动化这些过程吗?顶部栏中的用户名是动态的,所以我必须将它与视图链接,除非我不使用骨干网来管理它,但很遗憾,你怎么看?
  • 导航栏的一个视图,主要内容的另一个视图(或其他几个视图),以及管理页面本身的整体视图(它将仅包含其他视图)。
  • 好的,我明白了,但我从来没有看到过(对于管理所有视图的视图),你能给我看一个小例子或更新我的 Jsfiddle 吗?感谢您的帮助!
  • 我认为您正在寻找一个更像这样的结构:jsfiddle.net/ambiguous/4Bx6h

标签: javascript backbone.js backbone-views


【解决方案1】:

我在 window.homeView 中添加了对父级初始化的调用:http://jsfiddle.net/japNK/18/,如下所述:Accessing parent class in Backbone

【讨论】:

  • 我认为没有必要添加“窗口”。在父级初始化调用之前。但我没有看到任何变化,只有来自home-container 的“匿名”,但视图中仍然没有来自template-frontend-container 的“欢迎匿名”,我错了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-25
  • 2013-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多