仅客户端与 Meteor 的反应? [英] Client-side-only reactivity with Meteor?
问题描述
我在服务器上发布了一个集合,并在客户端上自动订阅.我想在会话中设置选定"项目并更新模板以仅显示选定项目,但这似乎只能通过往返服务器来完成(这是完全不必要的).
常见:
var Missions = new Meteor.Collection('missions');
客户:
Template.missionList.missions = function() {var currMission = Session.get('selectedMission');var searchMission = {};如果(当前任务){searchMission['_id'] = currMission;}返回 Missions.find(searchMission);};Template.missionList.events({'点击div.mission':函数(e,t){Session.set('selectedMission',this._id == Session.get('selectedMission') ?空:this._id);}});Template.mission.isSelected = function() {返回 this._id == Session.get('selectedMission');};Meteor.autosubscribe(函数(){Meteor.subscribe("任务");});
服务器:
Meteor.publish('任务', function() {//这里确实有一些过滤器,但为了简单起见删除了返回 Missions.find();});
模板:
<div class="missionList">{{#每个任务}}{{>使命}}{{/每个}}
模板><模板名称=任务"><div class="mission{{#if isSelected}} selected{{/if}}">详情</div>模板>
我的需求是让Template.missionList.missions
中的Missions.find()
过滤客户端缓存的结果,而不是重新请求服务器,但我似乎找不到允许我告诉 minimongo 仅使用当前可用数据的标志或设置.
我也不完全确定这是否是我应该做的,我一开始只是使用 jQuery 来隐藏未选择的任务,但我开始了解 Meteor,它似乎很适合使用数据和反应性推动选择/本地过滤.
有什么办法可以避免往返,还是我用错了?
通过设置发布/订阅关系,您正在创建一种简化形式的数据库复制.Minimongo 将在本地拥有一份数据副本并在本地执行 find() ,而无需服务器往返.如果您看到网络活动或对服务器代码的调用,那是因为meteor 定期在幕后工作以保持订阅与服务器同步,而不是为了您的特定发现.
这也意味着您必须警惕向客户端发送过多数据,因此您的服务器端发布功能除了现有您的选择标准外,还可能希望通过客户端所需的特定字段进行过滤.
I have a collection published on the server and auto-subscribed on the client. I'd like to set the 'selected' item on the session and have the template update to display only the selected item, but it seems this can only be done with a roundtrip to the server (which is totally unnecessary).
Common:
var Missions = new Meteor.Collection('missions');
Client:
Template.missionList.missions = function() {
var currMission = Session.get('selectedMission');
var searchMission = {};
if(currMission)
{
searchMission['_id'] = currMission;
}
return Missions.find(searchMission);
};
Template.missionList.events({
'click div.mission': function (e, t) {
Session.set('selectedMission',
this._id == Session.get('selectedMission') ? null : this._id
);
}
});
Template.mission.isSelected = function() {
return this._id == Session.get('selectedMission');
};
Meteor.autosubscribe(function () {
Meteor.subscribe("missions");
});
Server:
Meteor.publish('missions', function() {
// there are really some filters here, but removed for simplicity
return Missions.find();
});
Template:
<template name="missionList">
<div class="missionList">
{{#each missions}}
{{> mission}}
{{/each}}
</div>
</template>
<template name="mission">
<div class="mission{{#if isSelected}} selected{{/if}}">details</div>
</template>
My requirement is for the Missions.find()
in Template.missionList.missions
to filter the client-side cached results, rather than to re-request from the server, but I can't seem to find a flag or settings to allow me to tell minimongo to only use the currently available data.
I'm also not entirely sure if this is what I should be doing, I started out just using jQuery to hide the non-selected missions but getting my head round Meteor and it seems a natural fit to use the data and reactivity to drive selection/local-filtering.
Is there any way the roundtrip can be avoided or am I just using it wrong?
By setting up a publish / subscribe relationship, you are creating a simplified form of database replication. Minimongo will have a copy of the data locally and execute the find() locally without a server roundtrip. If you are seeing network activity or calls to the server code, it is because meteor is regularly working behind the scenes to keep the subscription in sync with the server, not for your specific find.
This also means you have to wary of sending too much data to the client, so your server side publish function may want to filter by the specific fields needed by client, in addition to existing your selection criteria.
这篇关于仅客户端与 Meteor 的反应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!