如何为数组嵌套对象子项获取Parent对象| MeteorJs - 天文学

时间:2016-01-11 17:17:01

标签: javascript meteor nested meteor-blaze

我正在使用MeteorJsAstronomy进行对象验证。

这是我的Users class

Emails = Astro.Class({
  name: 'Emails',
  fields: {
    address: {
      type: 'string',
      validator: [
        Validators.required(),
        Validators.email()
      ]
    },
    verified: {
      type: 'boolean',
      validator: Validators.required(),
    }
  }
})
Users = Astro.Class({
  name: 'Users',
  collection: Meteor.users,
  fields: {
    emails: {
      type: 'array',
      nested: 'Emails',
      default: function() {
        return [];
      }
    },
  }
})

现在,在Template JS文件中,当用户更改电子邮件时,我会从Blaze模板接收事件,但this链接到电子邮件对象,而不是用户1。谁可以在电子邮件更改事件中获取用户对象?

HTML

{{#each emails}}
  <input type="email" name="address-{{@index}}" value="{{address}}" data-email-idx="{{@index}}">
{{/each}}

JS

Template.userForm.events({
  'change input[type=email]': function (event) {
    var email = this;
    var field = event.currentTarget;
    let email_idx = field.getAttribute('data-email-idx')
    email.set('address', field.value)

    // let user = {...}?
  },
})

2 个答案:

答案 0 :(得分:0)

user = Template.parentData();

相关问题:Can i get the template parent data context in the event function?

您还可以使用{{../parent_field}}

访问HTML中的父数据上下文

答案 1 :(得分:0)

我和天文学老板谈过,他说现在不可能。所以,我正在关闭主题。

Git Issue

相关问题