【问题标题】:DOM manupulation in meteor流星中的DOM操作
【发布时间】:2015-06-14 06:46:34
【问题描述】:

我想知道流星中的 abt DOM 操作。我的代码如下:

    <template name = "studentList">

    {{#each}}
        <div class = "name">
            Name: {{this.name}}
        </div>

        <div class = "age">
            Age: {{this.age}}
        </div>
        <button class = "edit"> Edit </button>
    {{/each}}

    <button class = "addStudent"> Add Student </button>
</template>


Template.studentList.helpers({
    studentlist:function(){
        return Students.find();
    }
});

Template.studentList.events({
    //I am doing the DOM manupulation here based on the buttons clicked
});

我从数据库中获取学生信息列表并将它们显示在模板中。现在每个学生都有一个编辑按钮。当用户单击此编辑按钮时,我想将学生的“姓名”和“年龄”字段更改为文本字段,并提供“保存”和“取消”选项。

同样,我在模板末尾有一个“添加学生”按钮。当用户单击它时,我想显示一个表单,其中添加并保存了学生的姓名和年龄。

到目前为止,我能够做到这一点,但是通过在 studentList 的事件中使用大量 Jquery/Javascript 代码以一种非常幼稚的方式。我读了很多帖子说这不是正确的方法。

无论如何,请告诉如何在流星中实现此功能。或者只是一些可能的方法。

帮助表示赞赏。

【问题讨论】:

    标签: javascript jquery meteor meteor-blaze meteor-helper


    【解决方案1】:

    这是实现此目的的一种可能方式。

    让我们尝试逐步进行

    首先这是HTML 的外观。

    {{#if  editName}} <!-- editName template helper -->
       <!-- If the Session is equal to true this part of the html will be showed -->
        <input type="text" class="newName" value="{{this.name}}" />
      {{else}}
        <!-- this is what we show by default on the view, if user click the div, the input will be showed -->
        <div class="name">Name: {{this.name}} </div>
    {{/if}}
    

    现在是 JS

    助手应该是这样的。

    Template.studentList.helpers({tName:function(){
          return Session.get("studentName" + this._id); //getting the session true or false.
        }
    });
    

    还有事件。

    Template.studentList.events({
      'click .name' : function(event,template){
         //On this event we are Setting the Session to true and holding the id of the student
         return Session.set("studentName" + this._id,true)
       },
       'keypress .newName':function(event,template){
          //Some keypress to update the value 
           //using http://docs.meteor.com/#/full/template_$ to get the value using meteor
           var newNameStudent = template.$('.newName').val();
           if(event.keyCode == 13){ //if enter lets update the value
               Students.update({_id:this._id},{$set:{name:newNameStudent}})
               return Session.set("studentName" + this._id,false) //setting to false the session to hide the input and show the <div>
             }
         },
         'click .cancel':function(){
            return Session.set("studentName" + this._id,false) //setting to false the session to hide the input and show the <div>
           }
      })
    

    如果您看到没有太多代码(我猜),您就会知道如何使用会话来执行简单的 CRUD 操作。

    我为这个工作示例做了一个Meteorpad,检查一下。

    【讨论】:

    • 我自己按照上面的类似步骤,但我仍然接受你的回答,因为这是正确的方法:)
    猜你喜欢
    • 2014-06-07
    • 2015-03-27
    • 2010-09-06
    • 1970-01-01
    • 2011-09-12
    • 2018-12-13
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多