In my template, I call a server-side method to add an item to a collection. This collection is displayed on the page. Once the new item is rendered, I want to focus it for the user.
Here's the helper that fires the server method.
'click .add-post-button': function(e) {
var userId = Template.parentData(0)._id;
Meteor.call('addPost', userId, function(error, result) {
Session.set('newPostId', result);
});
}
Once this is finished, the new item appears. I want to focus it for the user. Previously, I tried to do it in the callback above with jQuery, but that didn't work because it had not been added to the DOM by the time the callback ran. Now, I'm trying to use the rendered callback:
Template.postsAdmin.rendered = function() {
var newPostId = Session.get('newPostId');
if (newPostId) {
var $newPostCell = $('#' + newPostId);
$newPostCell.find('.post').focus();
}
};
Unfortunately, this doesn't seem to be working either. This code does not run when the new item is added to the view.
How can I run code after the new item has been added to the view in order to focus the new item?