【问题标题】:Meteor Iron Router resubscribe on Rights change with meteor-rolesMeteor Iron Router 使用流星角色重新订阅权限更改
【发布时间】:2015-06-18 03:56:27
【问题描述】:

我正在使用用户角色系统 (alanning:roles) 编写一个流星应用程序,我的角色是基于组的。当用户知道我的组 url 时,它被允许访问该组并在该组中获取角色“defaultUser”。

允许localUser 订阅一个组的所有本地内容。

根据群号,我也发布了一些内容。

问题是:订阅没有重新订阅。

工作流程:

  1. 用户访问应用,
  2. 调用meteor-method获取默认角色
  3. 获取默认角色
  4. 订阅发布
  5. 如果用户具有正确的角色,则发布(完整组)发布内容

我的出版物看起来像:

Meteor.publish "thisGroupPublic", (id) ->
    return db.groups.find({_id: id}, {fields: {onlypublicones...}}

Meteor.publishComposite "thisGroupReactive", (id) ->
    return {
        find: () ->
            if !Roles.userIsInRole(@userId, "defaultUser", id)
                @ready()
                console.log("[thisGroupReactive] => No Rights")
                return;

            return db.groups.find({_id: id});

        children: [
            {
                find: (group) ->
                    return db.contents.find({groups: {$in: [group._id]}}, {fields: {apikey: 0}})
            }
        ]
    }

用户在登录页面上订阅订阅“thisGroupPublic”,并在第一次作为登录用户访问组时获得角色“defaultUser”。但是我需要如何配置iron:router 重新订阅此订阅以显示内容而不仅仅是公共内容?

【问题讨论】:

  • 要明确一点:用户在一条不会改变的路线上。当用户在这条路线上时,一些数据发生了变化,你想根据这个数据变化向路线发送一些东西来重新订阅一些东西,对吧?

标签: meteor iron-router


【解决方案1】:

说用户在/something的路线上

你有一些数据发生了变化,你创建了一个会话变量:

Session.set("someDataThatChanges", myChangedData)

您的发布函数接受某种输入,用于从集合中返回不同的数据:

Meteor.publish("myCollection", function(input){

  return myCollection.find( 
    // do something here based on 'input' 
  );

});

Iron Router 有一个与Meteor.subscribe 相同的.subscribe 方法,还有一个带有函数的subscriptions 键。您可以将Tracker.autorun 包裹在您的.subscribe 周围,然后放入您的会话变量,以根据该会话变量的变化值自动重新订阅某些内容。

Router.route("/something", {

  name: "templateName",

  // a place to put your subscriptions
  subscriptions: function() {

    console.log("this in router ", this);

    Tracker.autorun(function(){
      this.subscribe('myCollection', Session.get("someDataThatChanges");
    });

  },

});

【讨论】:

    猜你喜欢
    • 2015-09-21
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 2017-01-19
    • 2014-02-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    相关资源
    最近更新 更多