简体   繁体   中英

How to get Parent object for array nested object children | MeteorJs - Astronomy

I'm using MeteorJs with Astronomy for object validation.

This is my 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 [];
      }
    },
  }
})

Now, on Template JS file, I receive event from Blaze template when user change emails, but this is linked to email object, and not user one. Who can I get the user object on email change event ?

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 = {...}?
  },
})

user = Template.parentData();

Related question: Can i get the template parent data context in the event function?

You can also access the parent data context in the HTML by using {{../parent_field}} .

I talk to Astronomy owner, and he say it's not possible right now. So, I'm closing subject.

Git Issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM