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

发布于 2024-12-20 14:44:13 字数 2456 浏览 0 评论 0原文

大家好,当我在模型中添加一些东西时,按下按钮,zview 将显示新添加的人,我陷入了显示集合中我想要的视图的困境。下面是我的代码。

 $(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 代码

 <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>

任何有关语法和解释的帮助都是值得赞赏的。谢谢

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. 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

 <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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请你别敷衍 2024-12-27 14:44:13

除了查看 kinakuta 的评论之外,我建议查看以下内容:

首先,您应该从 zview 中删除以下代码,因为它是无效的 javascript。

model: Person,

然后,当您创建 zview 视图的实例时,您可以传递模型作为参数,因此您可以替换

var showit = new zview();

为:

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

然后,在渲染 zview 时,您可以直接引用模型。

尝试替换:

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

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

这至少应该让你开始走上正确的轨道。

In addition to looking at kinakuta's comment I'd suggest looking at the following:

First of all, you ought to remove the following code from zview as it is invalid 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

var showit = new zview();

with:

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

Then when rendering your zview you can refer directly to the model.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文