2014-04-23 35 views
4

嗨,我很新的烬js。我为新员工录入了一张表格,并通过路由发送数据。数据保存成功。但问题是表单提交后我的表单数据没有清除。如何清除余烬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:

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

我在计算器搜索这个我找到了一些解决方案,但他们使用的保存和退出函数。但我没有使用ember.i中的save方法直接保存在服务器端。

我的错误是什么。提交表单后,请提供一些建议以清除表单数据。

+0

能告诉我们你是如何实现你的表单的吗? –

+0

我添加了我的表单代码并更新了 –

+0

这是甚么工作?我没有看到使用你的NewprofileController的任何路由。通常,您需要在控制器中“手动”设置数据,因为控制器是“静态”的。所以每次Ember需要一个控制器,他都会得到同样的一个。这就是为什么在路径中有一个称为setupController的方法来重置控制器中的数据的原因。 –

回答

6

你必须做这样的控制器内部:

var controller = this; 

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

表单数据必须与像你的ID,名称和指定控制器结合的性质。

+0

谢谢您的正确回复。并且我还添加了错误。但错误发生时它不起作用 –