简体   繁体   English

如何显示骨干模型集合中的视图

[英]how to display view from model collection in backbone

hi guys i am stuck in displaying the view from the collection what i want when ever i add some thing into the model on button press the zview displays the newly added person. 嗨,大家好,当我在按钮上按按钮向模型中添加一些东西时,我一直想显示集合中的视图,按zview会显示新添加的人。 below is my code. 下面是我的代码。

 $(document).ready(function(){

    var Person = Backbone.Model.extend({
        name: 'default name',
        age: 100,
        initialize: function(){
            this.bind('change', this.render, this);
        }
    });

    var PersonCollection = Backbone.Collection.extend({
        model: Person
      });

    var PersonView = Backbone.View.extend({
        el: $('#personEL'),

        events: 
        {
            "click #login-btn" : "loginClick"
        },
        loginClick: function()
        {
          var name = $('#name').val();
          var age = $('#age').val();
          var newPerson = new Person({name: name, age : age});


          this.personCollection.add(newPerson);          
          var showit = new zview();

        },
        initialize: function(){
         this.personCollection = new PersonCollection();

           this.render(); 
        },

        render: function()
        {
                var template = _.template( $("#personInputTemplate").html(), {} );
        this.el.html(template);
        }
    });



     zview = Backbone.View.extend({
        el: $('#personTable'),

       initialize: function()
       {
        model: Person,
        this.render();   
       }
       ,
       render: function()
       {
           var template = _.template( $('#personDisplayTemplate').html(),this.Person.toJSON() );
           console.log(template);
          this.el.html(template);
       }
    });


var persons = new PersonView();

  });

html code HTML代码

 <div id="display">
        <table id="personTable">

        </table>
    </div>


<script type="text/template" id="personInputTemplate">
    <p>Person Name<input type="text" id="name" /></p>
    <p>Person Age<input type="text" id="age" /></p>
    <p><button id="login-btn">Save</button></p>
</script>

<script type="text/template" id="personDisplayTemplate">
            <tr>
                 <td><span><%= name ? name : '' %></span></td>
                 <td><span><%= age ? age : '' %></span></td>

            </tr>
</script>


<div id ="personEL"></div>
<div id="showPerson"></div>

any help with syntax and explanation is apprecciated. 感谢有关语法和解释的任何帮助。 thanks 谢谢

In addition to looking at kinakuta's comment I'd suggest looking at the following: 除了查看kinakuta的评论外,我建议查看以下内容:

First of all, you ought to remove the following code from zview as it is invalid javascript. 首先,您应该从zview中删除以下代码,因为它是无效的javascript。

model: Person,

Then, when you create an instance of your zview view you can pass a model as an argument, so you could replace 然后,当您创建zview视图的实例时,可以将模型作为参数传递,因此可以替换

var showit = new zview();

with: 与:

var showit = new zview({model: newPerson});

Then when rendering your zview you can refer directly to the model. 然后,在渲染zview时,您可以直接引用模型。

Try replacing: 尝试更换:

var template = _.template( $('#personDisplayTemplate').html(),this.Person.toJSON() );

with

var template = _.template($('#personDisplayTemplate').html(), this.model.toJSON());

That should at least get you started on the right track. 这至少应该使您开始正确的道路。

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

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