如何让 Meteor 对子项目有反应,但对父项目没有反应 [英] How to make Meteor reactive on sub-items, but not parent
问题描述
我有一个想用 Meteor 构建的 Newsfeed 想法,但我在弄清楚如何使新闻提要本身保持不变时遇到了一些困难,这不是反应性的,而是更新子项目(评论、喜欢等)一更新.
我将所有内容都存储在一个集合中,如果可能,我想保持这种状态.所以集合是这样设置的:
<预><代码>[{title: '随机标题',date_created: '01/01/2001',注释:[{'message': 'Lorem ipsum', date_created: '01/01/2001'},[...]]},[...]]所以我想做的是让新闻源无反应,这样当插入或更新新新闻时,保存新闻列表的模板不会被重新渲染.但是,如果添加、删除评论或有人喜欢新闻提要,我希望它立即在模板中更新.
我一直在想如何使用 {{#isolate}}
和 {{#constant}}
但没有成功.
这是我的客户端 JS:
Template.group_feed.feed_data = function() {var feed = Newsfeed.find({}, {排序:{updated_time:-1},限制:10,反应性:假}).拿来();回饲料;};
我设置了 reactive: false
以便它不会更新模板,但是当评论或喜欢被更新时,这也使它成为静态的.所以我猜有一种更好的方法可以做到这一点,然后使整个集合无反应.
这是我的模板代码:
<模板名称="group_feed"><div id="feed-wrapper"><ul>{{#each feed_data}}{{>group_feed_item}}{{/每个}}
模板><模板名称="group_feed_item"><li><h1>{{title}}</h1><div class="评论">{{#每个评论}}<p>{{message}}</p>{{/每个}}
模板>
有没有人有实现这一目标的好方法?
我没有测试它,但我想限制客户端的订阅是最直接的,从而减少数据传输并消除对保留的需要:
应该是这样的:
在服务器上:
Meteor.publish('tenItemsBefore',function (time) {Newsfeed.find({updated_time: {$lt time}}, {排序:{updated_time:-1},限制:10})}
在客户端上,在响应式上下文中,例如.在 Meteor.autorun() 中:
Newsfeed.subscribe('tenItemsBefore',Session.get('lastUpdate'));
在客户端,由事件触发,例如.使用路由器包刷新:
Session.set('lastUpdate', (new Date()).getTime());
希望有所帮助,
最好的,简
I've got a Newsfeed idea that I wanted to build with Meteor, but I'm having a bit of a struggle figuring out how to make the news feed itself constant, that is not reactive, but update the sub-items (comments, likes, etc) as soon as they're updated.
I've got everything stored in a single collection, and I'd like to keep it that way if possible. So the collection is setup like this:
[
{
title: 'A random title',
date_created: '01/01/2001',
comments:
[
{'message': 'Lorem ipsum', date_created: '01/01/2001'},
[...]
]
},
[...]
]
So what I'd like to do is have the newsfeed non-reactive, so that when a new news item is inserted or updated, the template holding the list of news won't get re-rendered. But if a comment is added, deleted, or someone likes the news feed, I'd want that to get updated right away in the template.
I've been trying to figure out how to use {{#isolate}}
and {{#constant}}
but to no prevail.
Here's my client side JS:
Template.group_feed.feed_data = function() {
var feed = Newsfeed.find({}, {
sort: {updated_time: -1},
limit: 10,
reactive: false
}).fetch();
return feed;
};
I set reactive: false
so that it doesn't update the template, but that makes it static also when comments or likes are updated. So I'm guessing there's a better way to do this then to make the whole collection non-reactive.
Here's my template code:
<template name="group_feed">
<div id="feed-wrapper">
<ul>
{{#each feed_data}}
{{> group_feed_item}}
{{/each}}
</ul>
</div>
</template>
<template name="group_feed_item">
<li>
<h1>{{title}}</h1>
<div class="comments">
{{#each comments}}
<p>{{message}}</p>
{{/each}}
</div>
</li>
</template>
Anyone got a nice way of achieving this?
I did not test it, but I guess it would be most straightforward to limit the subscription of the client, thus reducing data transfer an eliminataing the need for the preserve:
it would be something like:
on server:
Meteor.publish('tenItemsBefore',function (time) {
Newsfeed.find({updated_time: {$lt time}}, {
sort: {updated_time: -1},
limit: 10
})}
on client, in reactive context eg. in Meteor.autorun():
Newsfeed.subscribe('tenItemsBefore',Session.get('lastUpdate'));
on client, triggered by an event eg. refresh using router package:
Session.set('lastUpdate', (new Date()).getTime());
hope that helps,
best, Jan
这篇关于如何让 Meteor 对子项目有反应,但对父项目没有反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!