如何在渲染上从反应数据源正确设置复选框选中的属性

Cha*_*use 5 meteor iron-router

我有多个复选框,其checked属性表示集合中的布尔值.我的代码正确跟踪复选框上的click事件并更新集合中的值.但是,当页面首次加载时,以及当用户离开页面时,尽管会话和集合值正确,但未正确设置checked属性.

HTML

<input type="checkbox" id="feedEmailSubscribe" checked={{isChecked}}>
Run Code Online (Sandbox Code Playgroud)

助手JS

Template.layout.isChecked = function () {
  var id = $this.id;
  return Session.get(id) ? "checked" : "";
};
Run Code Online (Sandbox Code Playgroud)

我首先下拉集合并通过'邀请'路线为复选框设置会话变量

// invites
  this.route('invites', {
    path: ':_id/invited-guest/VIP',
    waitOn : function () {
      return Meteor.subscribe('guests', this.params._id);
    },
    data: function () {
      Session.set('guestId', this.params._id)
      return Guests.findOne({_id: this.params._id});
    },
    action : function () {
      var q = Guests.findOne({_id: this.params._id});
      if (this.ready()) {
        Session.set('guestName', q.name);
        Session.set('feedEmailSubscribe', q.feedEmailSubscribe);
        //...I then redirect the user
      }
    },
  });
Run Code Online (Sandbox Code Playgroud)

我可以验证会话设置.这是我对设置路线(复选框存在的位置)的所有内容

// settings page
  this.route('settings', {
    path: '/settings',
    waitOn : function () {
      return Meteor.subscribe('guests', Session.get('guestId'));
    },
    data: function () {
      return Guests.findOne({_id: Session.get('guestId')});
    },
    action: function () {
    if (this.ready())
        this.render();
    },
    onAfterAction: function() {
      setPageTitle('Settings');
    }
  });
Run Code Online (Sandbox Code Playgroud)

我可以验证会话是否存在以及在页面呈现后单击后它是否正确更改.我怀疑我是不是正确使用Blaze在Blaze wiki中简要讨论过,或者我的铁路由器设置有问题?

在此先感谢您的帮助!

Boz*_*hao 1

你的助手对我来说看起来不错,但是,我会返回 false,正如他们在此处记录的那样:

https://github.com/meteor/meteor/wiki/Using-Blaze#conditional-attributes-with-no-value-eg-checked-selected