I’m simulating a todo list where the textfield’s value gets saved in the model when it loses focus or when the user presses enter.
//view etc.
events:{
"blur .task": "doneEditing",
"keypress .task": "doneEditing"
},
doneEditing: function(e){
if(e.which && e.which != 13) return;
e.preventDefault();
//model saving code
}
The problem is that the keypress enter triggers doneEditing, and then the blur happens and triggers doneEditing again. I could use a bit of tricks to find a workaround, but I was wondering if backbone has a way of only triggering one of either event.
Thanks.
If those two events happen in a short time interval, you can just use
underscore.jslibrary’s (Backbone’s hard dependency, so you’ll have it anyway)throttle-method to stop too many calls in a short succession. Here is a link to the documentation.And an example:
Hopefully that helped!