【问题标题】:Meteor - data context in Template.created?Meteor - Template.created 中的数据上下文?
【发布时间】:2014-12-24 09:42:36
【问题描述】:

我正在开发一个共享日历应用程序。我有以下(简化的)模板结构:

<template name="meeting">
    {{> calendar}}
</template>

<template name="calendar">
    {{#each days}}
        {{> day}}
    {{/each}}
</template>

<template name="day">
    etc etc
</template>

我有一个用于每天状态的 ReactiveVar,设置如下:

Template.day.created = function(){
  this.state = new ReactiveVar();

  this.autorun(_.bind(function() {
    var thisDate = Meetings.findOne(this.data._id).dates[this.data.dateString];
    s = ...various stuff...
    this.state.set(s);
  }, this));
}

并且路由设置是这样的(我使用自己的 uuids 而不是 Mongo ids 作为公共 url):

Router.map(function () {
  this.route('home', {path: '/'});
  this.route('meeting', { 
    path: '/meeting/:uuid',
    template: 'meeting',
    data: function() {
      return Meetings.findOne({"uuid":this.params.uuid});
    }
  });
});

据我了解,核心问题是 Template.day.created 还没有数据上下文,所以我需要一种方法来传递会议的 _id(或 uuid)。提供此上下文的正确方法是什么?

【问题讨论】:

  • 数据上下文应该在您创建的回调中作为this.data 可用,因此您应该能够使用this.data._id。然而,这只是一个非反应性的只读值。
  • 对;但由于某种原因,在调用回调时,数据是null,所以我认为这可能会发生:stackoverflow.com/questions/25803169/…
  • @VirgilDisgr4ce,是的,您需要拥有数据(包含数据的订阅必须准备好),然后才能在模板中使用它。

标签: meteor


【解决方案1】:

您可以尝试这样做:

Router.map(function () {
  this.route('home', {path: '/'});
  this.route('meeting', { 
    path: '/meeting/:uuid',
    template: 'meeting',
    waitOn: function() {
      return Meteor.subscribe('Meetings');
    },
    data: function() {
      return Meetings.findOne({"uuid":this.params.uuid});
    }
  });
});

【讨论】:

    猜你喜欢
    • 2015-08-05
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    • 2018-08-26
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多