mercredi 23 mars 2016

Ember : Clear ember object attribute values

I am implementing crud operation in my Ember project. I passed an object to the component and tried to use the same object for save, update action.

In route I created model as follows,

routes.js

model() {
  return Ember.RSVP.hash({
        employeeList : this.store.findAll("employee"),
        employee : Ember.Object.create(),
      });
} 

component: employee-form.js

<form class="form-horizontal" role="form">
<div class="form-group">
<label class="control-label col-sm-3" for="fname">First Name:</label>
<div class="col-sm-8">
    {{input type="text" value=model.firstName placeholder="First Name"}}
 </div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="lname">Last Name:</label>
<div class="col-sm-8">
  {{input type="text" value=model.lastName placeholder="Last Name" }}
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="email">Email:</label>
<div class="col-sm-8">
   {{input type="text" value=model.email placeholder="Email" }}
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="department">Department: </label>
<div class="col-sm-8">
    {{input type="text" value=model.department placeholder="Department" }}
</div>
</div>
</form>

I am passing object to component as follows, Template.hbs

{{employee-form model=model.employee}}

So, I need to clear the attributes data in object for using it for different operation. I tried to replace the model object with a new ember object and it worked. But I don't think it is a nice way to do, as it is creating new object always.

this.set("model", Ember.Object.create());




Aucun commentaire:

Enregistrer un commentaire