Manu Benjamin
Manu Benjamin

Reputation: 997

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

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());

Can anyone help me with best approach to clear ember object attributes values.

Upvotes: 1

Views: 491

Answers (1)

murli2308
murli2308

Reputation: 2994

Use rollbackAttributes()

RollbackAttributes - If the model hasDirtyAttributes this function will discard any unsaved changes. If the model isNew it will be removed from the store.

model.rollbackAttributes();

Upvotes: 2

Related Questions