许多订阅准备就绪时的流星

时间:2015-06-10 10:36:40

标签: meteor

我正在创建一个聊天应用。如果我检查当前聊天的消息数等于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语言环境中获得另一个“问候消息”。

1 个答案:

答案 0 :(得分:1)

以下是在订阅准备好后,您只能渲染模板的方法。这是从meteor kitchen生成的代码中获取的解决方案。

  • 首先创建一个" loading"模板

    <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包。它很容易实现。