i am trying to update item on keypress enter without using save button but how to do ?
To call an action on âenterâ from an input you can do this:
{{input type=âtextâ enter=âyourActionâ}}
this is my code âŚi canât able to update without save option how can i update automatic save on keypress enter {{#if todo.isVisible}} {{input value = todo.name }} <button type =âbuttonâ class=âbtn btn-successâ {{action âupdateTodoâ todo}}>Save {{else}} <span {{action âeditTodoâ todo on=âdoubleClickâ}}> {{todo.name}} <button type =âbuttonâ class=âbtn btn-dangerâ {{action âdelTodoâ todo.id}}>X {{/if}}
There are a few things that youâll want to be aware of when setting up auto-save;
- Always include a
<form>
tag to deal with the âsubmitâ action, or you might end up submitting something else in your app. - Debounce your save action if you are saving on âkey-downâ, else youâll kill browser performance and overload your server.
- Cancel debounces if you end up saving via another way, eg: submit
- Pressing
[Enter]
on a<input type="text">
field causes the<form>
s âsubmitâ event to fire
With that in mind, here is how I typically handle this (also using ember-data):
// app/controllers/foobar.js
import Ember from 'ember';
export default Ember.Controller.extend({
saveModel: function(){
var model = this.get('model');
// Avoid un-needed `.save()`s if user clicks Submit w/o typing anything
if (model.get('hasDirtyAttributes')) {
model.save().then( function(){
// handle success
}, function( error ){
// handle failure
});
}
},
actions: {
modelChanged: function(){
var debounced = Ember.run.debounce(this, this.saveModel, 2000); // 2 seconds
this.set('debounced', debounced);
},
submitForm: function(){
var debounced = this.get('debounced');
if (debounced) {
Ember.run.cancel( debounced );
}
this.saveModel();
}
} // :actions
});
<!-- app/templates/foobar.hbs -->
<form {{action "submitForm" on="submit"}}>
{{input type="text" value=model.attributeName key-press="modelChanged"}}
<button type="submit">Save</button>
</form>
I also âbuffer changesâ but thatâs kind of a different topic. This is the basic logic behind my âauto-saveâ feature.
This saved me so much time (should be added to the official docs). Once catch is this.saveModal should be stored as this.get(âsaveModalâ) to prevent a this context error.
What you mean @ashblue? What this context error?