[英]How do I run client-side code after a new collection item has been added to a Meteor view?
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.
以前,我尝试使用jQuery在上面的回调中执行此操作,但这不起作用,因为它在回调运行时尚未添加到DOM。 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? 在将新项目添加到视图中以便聚焦新项目后,如何运行代码?
Your original code is really close - you just need to put the focus logic into a reactive context so it will run again with the session variable changes. 您的原始代码非常接近 - 您只需将焦点逻辑放入反应式上下文中,以便在会话变量更改时再次运行。 The shortest path to success is to use a template autorun :
成功的最短途径是使用模板自动运行 :
Template.postsAdmin.onRendered(function() {
this.autorun(function() {
var newPostId = Session.get('newPostId');
if (newPostId) {
var $newPostCell = $('#' + newPostId);
return $newPostCell.find('.post').focus();
}
});
});
However, one problem you may run into is that you'll have a race condition where the new post is added after the autorun fires. 但是,您可能遇到的一个问题是,您将遇到一个竞争条件,即在自动运行之后添加新帖子。 A hacky solution is to add a timeout to the above.
一个hacky解决方案是在上面添加一个超时。 An improved solution is to add something like your original code to the
onRendered
callback of the newly added post sub-template: 一个改进的解决方案是将类似原始代码的内容添加到新添加的post子模板的
onRendered
回调中:
Template.post.onRendered(function() {
var newPostId = Session.get('newPostId');
if (newPostId === this.data._id) {
// add code here to focus this template instance
}
});
One option may be observeChanges
? 一种选择可能是
observeChanges
? Can you try this? 你能试试吗?
in the rendered function do following 在渲染函数中执行以下操作
Template.postsAdmin.rendered = function(){
var query = Posts.find({}); //your query
var handle = query.observeChanges({
added: function (id, user) {
//code to set focus???
}
});
}
This code runs whenever there is a new item addeded to minimongo which matches your query. 只要有一个新项目添加到与您的查询匹配的minimongo,就会运行此代码。
Make sure to show the template after you load the data. 加载数据后,请务必显示模板。
docs link http://docs.meteor.com/#/full/observe_changes docs链接http://docs.meteor.com/#/full/observe_changes
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.