![](/img/trans.png)
[英]How do you store data server side that is specific to a client in Meteor?
[英]How do you conditionally send data to the client in Meteor?
我正試圖弄清楚如何在流星中有條件地向客戶端發送數據。 我有兩種用戶類型,根據用戶的類型,它們在客戶端上的接口(因此它們所需的數據是不同的)。
讓我們說用戶是型counselor
或student
。 每個用戶文檔都有類似role: 'counselor'
或role: 'student'
。
學生有學生特定的信息,如sessionsRemaining
和counselor
,輔導員有像pricePerSession
等。
我如何確保客戶端的Meteor.user()
具有我需要的信息,而且沒有額外的信息? 如果我以學生Meteor.user()
登錄, Meteor.user()
應該包括sessionsRemaining
和counselor
,但如果我以輔導員的身份登錄,則不會。 我認為我可能正在尋找的是有條件的出版物和流星術語中的訂閱。
使用fields選項僅返回Mongo查詢所需的字段。
Meteor.publish("extraUserData", function () {
var user = Meteor.users.findOne(this.userId);
var fields;
if (user && user.role === 'counselor')
fields = {pricePerSession: 1};
else if (user && user.role === 'student')
fields = {counselor: 1, sessionsRemaining: 1};
// even though we want one object, use `find` to return a *cursor*
return Meteor.users.find({_id: this.userId}, {fields: fields});
});
然后在客戶端上打電話
Meteor.subscribe('extraUserData');
訂閱可以在Meteor中重疊。 因此,這種方法的優點在於,向客戶端提供額外字段的發布功能與Meteor的幕后發布功能一起工作,該功能發送基本字段,如用戶的電子郵件地址和配置文件。 在客戶端上, Meteor.users
集合中的文檔將是兩組字段的並集。
默認情況下,Meteor用戶只發布其基本信息,因此您必須使用Meteor.publish手動將這些字段添加到客戶端。 值得慶幸的是, 發布的Meteor文檔有一個示例,向您展示如何執行此操作:
// server: publish the rooms collection, minus secret info.
Meteor.publish("rooms", function () {
return Rooms.find({}, {fields: {secretInfo: 0}});
});
// ... and publish secret info for rooms where the logged-in user
// is an admin. If the client subscribes to both streams, the records
// are merged together into the same documents in the Rooms collection.
Meteor.publish("adminSecretInfo", function () {
return Rooms.find({admin: this.userId}, {fields: {secretInfo: 1}});
});
基本上,您希望在滿足條件時發布將某些信息返回給客戶端的通道,而在不滿足條件時則發布其他信息。 然后,您在客戶端上訂閱該頻道。
在您的情況下,您可能希望在服務器中使用以下內容:
Meteor.publish("studentInfo", function() {
var user = Meteor.users.findOne(this.userId);
if (user && user.type === "student")
return Users.find({_id: this.userId}, {fields: {sessionsRemaining: 1, counselor: 1}});
else if (user && user.type === "counselor")
return Users.find({_id: this.userId}, {fields: {pricePerSession: 1}});
});
然后訂閱客戶端:
Meteor.subscribe("studentInfo");
因為Meteor.users是一個像任何其他Meteor集合一樣的集合,你實際上可以像任何其他Meteor集合一樣優化其公開內容:
Meteor.publish("users", function () {
//this.userId is available to reference the logged in user
//inside publish functions
var _role = Meteor.users.findOne({_id: this.userId}).role;
switch(_role) {
case "counselor":
return Meteor.users.find({}, {fields: { sessionRemaining: 0, counselor: 0 }});
default: //student
return Meteor.users.find({}, {fields: { counselorSpecific: 0 }});
}
});
然后,在您的客戶端:
Meteor.subscribe("users");
因此, Meteor.user()
將根據登錄用戶的角色自動截斷。
這是一個完整的解決方案:
if (Meteor.isServer) {
Meteor.publish("users", function () {
//this.userId is available to reference the logged in user
//inside publish functions
var _role = Meteor.users.findOne({ _id: this.userId }).role;
console.log("userid: " + this.userId);
console.log("getting role: " + _role);
switch (_role) {
case "counselor":
return Meteor.users.find({}, { fields: { sessionRemaining: 0, counselor: 0 } });
default: //student
return Meteor.users.find({}, { fields: { counselorSpecific: 0 } });
}
});
Accounts.onCreateUser(function (options, user) {
//assign the base role
user.role = 'counselor' //change to 'student' for student data
//student specific
user.sessionRemaining = 100;
user.counselor = 'Sam Brown';
//counselor specific
user.counselorSpecific = { studentsServed: 100 };
return user;
});
}
if (Meteor.isClient) {
Meteor.subscribe("users");
Template.userDetails.userDump = function () {
if (Meteor.user()) {
var _val = "USER ROLE IS " + Meteor.user().role + " | counselorSpecific: " + JSON.stringify(Meteor.user().counselorSpecific) + " | sessionRemaining: " + Meteor.user().sessionRemaining + " | counselor: " + Meteor.user().counselor;
return _val;
} else {
return "NOT LOGGED IN";
}
};
}
和HTML:
<body>
<div style="padding:10px;">
{{loginButtons}}
</div>
{{> home}}
</body>
<template name="home">
<h1>User Details</h1>
{{> userDetails}}
</template>
<template name="userDetails">
DUMP:
{{userDump}}
</template>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.