【问题标题】:Meteor when many subscriptions are ready许多订阅准备就绪时的流星
【发布时间】:2015-06-10 10:36:40
【问题描述】:

我正在创建一个聊天应用程序。如果我检查当前聊天的消息计数等于 0,我希望我可以添加一个新的“你好”消息(问题 #1)。我也有一本字典作为翻译的集合。但是 t() 返回 EN 变体(问题 #2)

t = function(text) {
  var res = Dictionary.findOne({o:text});
  return res && res.t || text;
}

Meteor.startup(function () {

Deps.autorun(function () {

  Meteor.subscribe('dictionary', Session.get('lang'), function(){
    Session.set('dictionaryReady', true);
  });

  Meteor.subscribe('chats', Session.get('domain'), function(){

    if (chatCurrent(Meteor.userId(), Session.get('domain')).count()===0 //true, even is not actually [problem_#1]
      && Session.get('dictionaryReady') //true, but next function t() doesn't work properly [problem #2]
      ) {
      var mudata = Session.get('my_manager') ? udata(Session.get('my_manager'), Session.get('domain')) : null,
        hello = mudata && mudata.hello || t('Hello! How I can help you?'),
        name = mudata && mudata.name || t('Anna');
      Meteor.call('create_message', {chat: Meteor.userId(), to: Meteor.userId(), text: hello, name: name, from: Session.get('my_manager'), domain: Session.get('domain'), last_manager: Session.get('my_manager')});
    });

  });

});

每次刚加载页面时都会出现问题 #1 和问题 #2。因此,当我刷新页面时,我会在默认 EN 语言环境中收到另一条“hello message”。

【问题讨论】:

    标签: meteor


    【解决方案1】:

    只有在您的订阅准备就绪后,您才能呈现您的模板。这是取自meteor kitchen 生成代码的解决方案。

    • 首先你创建一个“加载”模板

      <template name="loading"> <div class="loading"> <i class="fa fa-circle-o-notch fa-4x fa-spin"></i> </div> </template>

    • 其次,将路由控制器附加到您的模板。这是它的简化版本(但它应该可以工作):

      this.myTemplateController = RouteController.extend({

      template: "myTemplate",
      
      onBeforeAction: function() {
          this.next();
      },
      
      action: function() {
          if(this.isReady()) { this.render(); } else { this.render("loading"); }
      },
      
      isReady: function() {
      
          var subs = [
              Meteor.subscribe("sub1", this.params.yourParam),
              Meteor.subscribe("sub2", this.params.yourParam),
              Meteor.subscribe("sub3", this.params.yourParam)
          ];
          var ready = true;
          _.each(subs, function(sub) {
              if(!sub.ready())
                  ready = false;
          });
          return ready;
      },
      
      data: function() {
      
      
          return {
              params: this.params || {},
              yourParamWhatever: Chat.findOne({_id:this.params.yourParam}, {})
          };
      },
      

      });

    现在您应该在加载模板时准备好所有订阅。

    关于翻译,你可以看看我强烈推荐的TAPi18n 包。这很容易实现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-30
      • 2017-12-02
      • 1970-01-01
      • 2015-06-29
      • 1970-01-01
      • 2012-02-15
      • 1970-01-01
      • 2019-10-09
      相关资源
      最近更新 更多