Meteor Collection转换:是在服务器上完成还是在客户端上完成?或者取决于情况
Meteor Collection Transform: is it done on the server or on the client? or it depends
我想使用transform从集合中生成一个"虚拟字段"。然而,我正在添加的新字段(在transform函数中)向返回的文档添加了相当多的数据。
如果转换是在客户端内部进行的,这是可以的。如果它是在服务器端完成的,那么就会有带宽问题。
所以我想知道转换是在服务器上还是在客户端上完成的,还是取决于我如何查找/获取文档?
UPDATE:可以在服务器上进行转换。
你可以在客户端上进行这样的转换:
return YourCollection.find({}, {transform: function (doc) {
doc.test = true;
return true;
}});
Meteor在发布的查询(从Meteor.publish
内)上忽略transform
。客户端看到文档时,就好像转换不存在一样。
如果你想在服务器上使用转换,你可以这样做:
YourCollection = new Mongo.Collection("collection_name");
Meteor.publish("yourRecordSet", function() {
//Transform function
var transform = function(doc) {
doc.date = new Date();
return doc;
}
var self = this;
var observer = YourCollection.find().observe({
added: function (document) {
self.added('collection_name', document._id, transform(document));
},
changed: function (newDocument, oldDocument) {
self.changed('collection_name', newDocument._id, transform(newDocument));
},
removed: function (oldDocument) {
self.removed('collection_name', oldDocument._id);
}
});
self.onStop(function () {
observer.stop();
});
self.ready();
});
您可以添加并使用以下函数:
Meteor.publishWithTransform = function(publicationName, cursorGetter , transform)
{
transform = transform || function(o){return o;};
Meteor.publish(publicationName, function ()
{
var cursor = cursorGetter.apply(this, arguments);
var collectionName = cursor._cursorDescription.collectionName;
var self = this;
var observer = cursor.observe({
added: function (document) {
self.added(collectionName, document._id, transform(document));
},
changed: function (newDocument, oldDocument) {
self.changed(collectionName, newDocument._id, transform(newDocument));
},
removed: function (oldDocument) {
self.removed(collectionName, oldDocument._id);
}
});
self.onStop(function () {
observer.stop();
});
self.ready();
});
}
用法(服务器端):
Meteor.publishWithTransform("publication-name", function () {
aCollection.find({})
}, function (item)
{
item.aField = "something";
return item;
});
缺点:如果保存已发布的文档,也将保存添加的更改。在这里,客户端不知道在发布过程中添加了属性"aField"。
当您为集合或findOne、fetch等指定转换选项时,您可以在两侧使用转换
transform函数
可选的转换函数。在从fetch或findOne返回文档之前,以及在传递给observe、allow和deny的回调之前,文档将通过此函数传递。
如果您需要从具有转换选项的集合中获取原始文档。
myCollection.findOne({},{transform:null})
您也可以直接在集合定义中添加转换。注意:如上所述,这会在更新时将额外数据存储到集合中。对于只读收藏来说,这并不是一个真正的问题。
我也希望它能存在于共享的lib文件夹或类似的文件夹中,客户端和服务器都可以看到这个定义。
var Assignment;
// Every record in this collection will now be an
// instance of the Assignment class.
this.AssignmentsReport = new Mongo.Collection("assignments", {
transform: function(doc) {
return new Assignment(doc._id, doc.assignId, doc.masterId);
}
});
// Creating a class instance to actually map the new object.
Assignment = (function() {
function Assignment(_id, assignId, masterId) {
var assign, ref;
this._id = _id;
this.assignId = assignId;
this.masterId = masterId;
this.assign = (ref = Assignments.find({
_id: this.assignId
}).fetch()) != null ? ref[0] : void 0;
}
return Assignment;
})();
还有一个流星包,它允许在服务器端添加用于发布的转换https://atmospherejs.com/maximum/server-transform
- 如何删除除冒号、数字和'上午'或者'下午'
- jquery移动对齐按钮取决于内容大小
- 如何“;过滤器”;或者以其他方式重构该数据
- 动态更改'汉堡包'导航取决于BG图像
- Rubaxa可与聚合物进行排序/拖放不起作用,具体取决于显示器:
- jquery切换取决于文本框的值
- 哪个布尔运算更快<或者<=
- 多个jQuery.on()事件无法运行xBrowser(取决于顺序)
- 在编译阶段后创建新的DOM树,或者继续使用原始修改的DOM
- 或者在表单上选择默认选项文本(选择1)
- JavaScript-取决于多个下拉选择或复选框的动态链接
- 试图修改此javascript代码以减小弹出窗口的大小,或者在用户单击框外时关闭
- JQuery是否有一个“;移动“;作用或者有没有一种更紧凑的方法来做到这一点
- 咖啡'@'或者这在函数和类中
- Bootstrap CSS和JS我必须将它们包含在单独的文件中,或者可以编译它们
- 计时器在使用 jQuery 一分钟后没有触发,或者每 1 分钟后给出一些其他解决方案来触发功能
- 使用CloudFront和AmazonS3,或者其他CDN,我可以将所有类似的请求发送到一个静态文件中
- 下拉值取决于单选按钮的选择
- 表单上预先选择的选项取决于您的链接'We’’’’来自
- Meteor Collection转换:是在服务器上完成还是在客户端上完成?或者取决于情况