将结构化的,被动的数据发布到客户端(数据库集合之外) [英] Publishing structured, reactive data to the client (outside of database collections)

查看:62
本文介绍了将结构化的,被动的数据发布到客户端(数据库集合之外)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找性能最佳的解决方案,用于根据请求向Meteor框架中的客户端发送结构化数据。

I am looking for the most performant solution for sending structured data to the client in the Meteor framework on a request.

问题:
有时,在将数据从数据库发送到客户端之前,您希望添加一些服务器端生成的发送给客户端的附加信息(即许多对象的安全凭证)。此数据可能是时间关键的(即由于到期时间戳),因此不应存储在数据库中。而且,有时不能在客户端处理该数据(即由于安全原因)。在许多情况下,这些数据在结构上与实际数据库数据相关,但也与单个请求非常相关,因为您可能希望将其丢弃并在新请求中重新生成。

THE ISSUE: Sometimes, before you send data from the database to the client, you want to add some server side generated additional information beeing sent to the client (i.e. security credentials for many objects). This data can be time-critical (i.e. due to an expiration timestamp) and therefore should not be stored in the db. Also, this data sometimes cannot be processed on the client side (i.e. due to security reasons). In many cases, this data will be structurally related to actual database data, but also very much related to a single request, since you might want to have it discarded and re-generated on a new request.

你可以(至少按设计......):


  1. 创建第二个集合,在那里存储和发布与请求相关的数据,接受写入开销,然后在 Meteor.myTemplate.destroyed = function(){...} 再次删除数据,接受另一个写入开销。

  1. create a second collection, store and publish your request-related data there, accepting the write-overhead, and then i.e. in the Meteor.myTemplate.destroyed=function(){...} remove the data again accepting another write-overhead.

将每个条目存储在会话变量中,但是你还必须小心
稍后删除它( Meteor.myTemplate.destroyed = function(){...} ),这是我现在最喜欢的,但我遇到了问题在那里存储大型对象。

store each entry in a session variable, but then you also have to take care of deleting it later on (Meteor.myTemplate.destroyed=function(){...}), this is my favourite right now, but I am running into problems with storing large objects there.

你不能:(按设计!!)


  1. 服务器上的Meteor.publish(name,function(){...})

  2. 使用 Meteor.call () Template.variable = function(){return collection.find(...)} 的转换中(如果你不是在客户端上有一个相应的 Meteor.method()来猜测结果!)。

  1. use transformations within Meteor.publish("name",function(){...}) on the server
  2. use a Meteor.call() within a transformation on a Template.variable=function(){return collection.find(...)} (also not if you have a corresponding Meteor.method() on the client for guessing the result!).

同样,我正在寻找的是性能最佳的解决方案。

Again, what I am looking for is the best performing solution for this.

推荐答案

Jim Mack在这里创建了一个很好的例子,证明了它存储db数据以及其他转换的效果如何Session变量中的属性。
不幸的是,这个例子缺乏反应性,并且在Meteor的魔法重新渲染之后没有执行所需的转换。因此,我抓住了他的酷代码并添加了反应性,这是一个非常好的代码,但在效率方面将优于Jim Mack的例子。

Jim Mack created a nice example here that proves, how well it works to store db data as well as additional "transform" properties in a Session variable. Unfortunately this example lacks reactivity and does not perform the desired "transformations" after Meteor's magic re-render. So I grabbed his cool code and added back the reactivity, it's slim code that works very well, but will be outperformed by Jim Mack's example in terms of efficiency.

lolz.html

<head>
  <title>lolz</title>
</head>

<body>
  {{>myItems}}
</body>

<template name="myItems">
    <h3>Reactive Item List with additional properties</h3>
    <button id="add">add</button>
    <button id="remove">remove</button>
    <dl>
        {{#each items}}
            <dt>data: {{caption}}</dt>
            <dd>added property: {{anotherProp _id}}</dd>
        {{/each}}
    </dl>
</template>

lolz.js

items = new Meteor.Collection('Items');

if (Meteor.isServer) {
    items.allow({
    insert: function (userid, doc) {
        return true;
    },
    remove: function(userid,doc){
        return true;
    }
  });
  while(items.find().count()>0){
    items.remove(items.findOne()._id);
  }
  while (items.find().count() < 3) {
        items.insert({caption: 'LOLCATZ RULZ'});
    }
  Meteor.publish('Items', function () {
      return items.find();
  });
  Meteor.methods({
        'getAdditionalProps': function() {
            additionalProps={};
        items.find().forEach(function(doc){
            additionalProps[doc._id]=reverse(doc.caption);
      });
      return additionalProps;
    }
  });

    function reverse(s){ // server side operation, i.e. for security reasons
    return s.split("").reverse().join("");
    };
}

if (Meteor.isClient){
    Meteor.subscribe('Items');

    Meteor.startup(function(){
        getAdditionalProps();
        itemsHandle=items.find().observe({
            added : function(doc){
                getAdditionalProps();
            },
            removed : function(doc){
                getAdditionalProps();
            },
            changed : function(docA,docB){
                getAdditionalProps();
            }
        });
    });

    Template.myItems.rendered=function(){
        console.log(new Date().getTime());
    };

    Template.myItems.items=function(){
        return items.find();
    }

    Template.myItems.anotherProp=function(id){
        return Session.get('additionalProps')[id];
    }

    Template.myItems.events({
        'click #add':function(e,t){
            items.insert({caption: 'LOLCATZ REACTZ'});
        },
        'click #remove':function(e,t){
            items.remove(items.findOne()._id);
        }
    });
}

function getAdditionalProps(){
    setTimeout(function(){
        Meteor.call('getAdditionalProps',function(error,props){
            Session.set('additionalProps',props);
        });
    },0);
}

这篇关于将结构化的,被动的数据发布到客户端(数据库集合之外)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆