简体   繁体   English

如何在ember js中清除表单数据

[英]How can clear form data in ember js

Hi i am very new to ember js. 嗨,我是非常新的ember js。 i wrote a form for new employee entry.and send data through route.Data saved successfully. 我为新员工输入了一个表格,并通过route.Data发送数据成功保存。 But the problem is after form submission my form data not cleared. 但问题是在表单提交后我的表单数据没有被清除。

The code as follows: 代码如下:

app.js: app.js:

App.Router.map(function() {
    this.resource('saveprofile', { path: '/saveprofile/:profiledata'});
});

App.NewprofileController = Ember.ObjectController.extend({
    id:'',
    name: '',
    designation: '',

    saveProfileAction: function () {

        profiledata = [{ "id": this.get("id")}, 
        {"name": this.get("name")}, 
        {"designation": this.get("designation")}]

        pdata = $.ajax({
            type: "POST",
            dataType: "json",
            url: "/saveprofile?profiledata=" +profiledata,
            data: JSON.stringify(profiledata),
            dataType: "json",
            async: false}).done(function() {
                $.bootstrapGrowl("Employee added successfully", {
                    type: 'success',
                    align: 'center',
                    width: '1000',
                    allow_dismiss: false
                }).responseJSON
            })
        });

    console.log(pdata)
}
});

App.SaveProfileRoute = Ember.Route.extend({
    model: function(params) {
        console.log(params);
        return Ember.$.getJSON( "/saveprofile?profiledata=" + params.profiledata);
    },
})

index.html: index.html的:

     <script type="text/x-handlebars"  data-template-name="newprofile">
      <div class="panel panel-primary">
       <div class="panel-heading">
           <h3 class="panel-title">New Profile</h3>
       </div>
       <div class="panel-body">
         <form class="form-horizontal" role="form" id="form">
         <br/><br/>
       <div class="control-group">
        <label class="control-label" for="value">Employee Id:</label>
       <div class="controls"> 
            {{input type="text" class="input-medium" value=id required="true"}}
       </div>
       </div>
       <div class="control-group">
        <label class="control-label" for="value">Employee Name:</label>
       <div class="controls"> 
            {{input type="text" class="input-medium"  value=name}}
       </div>
       </div>
       <div class="control-group">
        <label class="control-label" for="value">Designation:</label>
       <div class="controls"> 
            {{input type="text" class="input-medium" value=designation}}
       </div>
       </div>      
       <div class="control-group pull-left">
        <div class="controls">
         <button type="button" class="btn btn-primary" {{action "saveProfileAction" }} id="button">Save</button>
          <button type="button" class="btn btn-primary" {{action "cancel"}} id="button">Reset</button>
        </div>
       </div>
         </form>
      </div>
     </div>
  </script>

I searched for this in stackoverflow i find some solutions but they use save and exit functions.but i did not use save method in ember.i directly save at server side. 我在stackoverflow中搜索了这个,我找到了一些解决方案,但是他们使用了save和exit函数。但是我没有在ember.i中使用save方法直接保存在服务器端。

what is my mistake. 我的错是什么 Give me some advise to clear the form data after form submission. 给我一些建议,在表单提交后清除表单数据。

You have to do it inside the controller like this: 你必须在控制器内部这样做:

var controller = this;

//After saving the form to the server:
...then(function(){
 controller.set('YourFormData', '');
});

Your Form Data has to be a property with binding in the controller like your id,name and designation. 您的表单数据必须是在控制器中具有绑定的属性,如您的ID,名称和名称。

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

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