【问题标题】:Updating subscribe data during session in Meteor在 Meteor 的会话期间更新订阅数据
【发布时间】:2015-12-14 02:16:03
【问题描述】:

我正在尝试更新传递给 Meteor 中 subscribe 参数的 id - 简而言之,我想使用当前 url 中的 id 订阅 Collection 对象的任何内容。我可以在第一次加载时发生这种情况,但不是在创建新对象并且用户在其会话中被路由到该新对象时。

我能够让这个模糊工作的唯一方法是使用setInterval 进行破解 - 我知道这不是很好,所以我正在寻找正确的 Meteor 方法。

目前我在server 中的publications.js 是:

Meteor.publish('shares', function(id){
 return Shares.find({_id:id});
});

router.js

Router.route('/share-social/:_id', {
 name: 'shareSocial',
 data: function() { return Shares.findOne(this.params._id); }
});

并通过main.js in client 订阅:(即,获取 url 中的 id,然后订阅它)

setInterval(function(){
 var x = location.href.split('/');
 var id = x.slice(-1).pop();
 Meteor.subscribe('shares', id);
}, 100);

最后是模板助手:

Template.shareSocial.helpers({
  shares: function() {
   return Shares.find().fetch();
 }
});

以上显然有其问题。如何仅响应式订阅具有当前 url 中显示的 id 的对象,无论它是在同一个会话中还是在新会话中?

谢谢!

【问题讨论】:

    标签: javascript meteor


    【解决方案1】:

    您可以通过将当前 id 定义为 Session 变量来为您的订阅设置响应式源,而不是从 URL 中分割 id,例如:

    // router.js
    Router.route('/share-social/:_id', function() {
        Session.set('id', this.params._id);
        this.render('shareSocial');
    });
    
    // main.js
    Tracker.autorun(function() {
        Meteor.subscribe('shares', Session.get('id'));
    });

    现在每次更改路由的 id 时,Session 变量和订阅都会更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-30
      • 1970-01-01
      • 1970-01-01
      • 2014-10-10
      • 2017-08-20
      • 1970-01-01
      • 2019-06-05
      • 2014-01-09
      相关资源
      最近更新 更多