Reputation: 45285
I just wondering if there is a way to refactore this code:
Template.bookmarks.events({
// save changes
'keyup .set_tag' : function(e,t) {
if(e.which === 13) {
save_changes(e.target.id.substring(3));
}
},
'keyup .set_name' : function(e,t) {
if(e.which === 13) {
save_changes(e.target.id.substring(4));
}
},
'click .save_changes' : function(e,t) {
save_changes(e.target.id);
}
});
Upvotes: 0
Views: 72
Reputation: 2820
Try wrapping in a form tag and listen to that form's submit event. Make sure to event.preventDefault()
You can then avoid all the keycode 13 stuff.
Upvotes: 2
Reputation: 7863
function action(key, offset) {
return function(e, t) {
if (e.which === key) {
save_changes(e.target.id.substring(offset))
}
}
}
Template.bookmarks.events({
// save changes
'keyup .set_tag': action(13, 3),
'keyup .set_name': action(13, 4),
'click .save_changes': function(e, t) {
save_changes(e.target.id);
}
});
One way to solve it. I wouldn't try to force the third event handler into the action
function as it would make the action
function more complicated.
Upvotes: 1