【问题标题】:How to get array index of nested object in autoform?如何在autoform中获取嵌套对象的数组索引?
【发布时间】:2015-11-27 16:28:52
【问题描述】:

我需要设置我的子类别的 slugs,我为此使用自动值。当我使用数组时,我需要知道实际字段的索引。这有通配符吗?

例如:

子类别.$.subs.$.name

  subcategories: {
    type: [Object],
    optional: true,
  },
  "subcategories.$.name": {
    type: String,
    optional: true,
  },
  "subcategories.$.slug": {
    type: String,
    optional: true,
    autoform: {
      omit: true
    },
    autoValue: function() {
      if (this.field('subcategories.$.name').isSet) {
        return s.slugify( (this.field('subcategories.$.name').value).toLowerCase() );
      }
    },
  },
  "subcategories.$.subs": {
    type: [Object],
    optional: true,
  },
  "subcategories.$.subs.$.name": {
    type: String,
    optional: true,
  },
  "subcategories.$.subs.$.slug": {
    type: String,
    optional: true,
    autoform: {
      omit: true
    },
    autoValue: function(i) {
      if (this.field('subcategories.$.subs.$.name').isSet) {
        return s.slugify( (this.field('subcategories.$.subs.$.name').value).toLowerCase() );
      }
    },
  },

谢谢,

【问题讨论】:

    标签: meteor meteor-autoform meteor-collection2


    【解决方案1】:

    我通过更改架构得到了预期的结果。除了变得更有条理之外,this.siblingField('name') 还有效!

    代码:

    Categories = new Mongo.Collection('categories');
    Schemas = {};
    
    Schemas.Subs = new SimpleSchema({
      "name": {
        type: String,
        optional: true,
      },
      "slug": {
        type: String,
        optional: true,
        autoform: {
          omit: true,
        },
        autoValue: function() {
          if (this.siblingField('name').isSet) {
            return s.slugify( (this.siblingField('name').value).toLowerCase() );
          }
        },
      },
    });
    
    Schemas.Subcategories = new SimpleSchema({
      "name": {
        type: String,
        optional: true,
      },
      "slug": {
        type: String,
        optional: true,
        autoform: {
          omit: true,
        },
        autoValue: function() {
          if (this.siblingField('name').isSet) {
            return s.slugify( (this.siblingField('name').value).toLowerCase() );
          }
        },
      },
      "subs": {
        type: [Schemas.Subs],
        optional: true,
      },
    });
    
    Schemas.Categories = new SimpleSchema({
      name: {
        type: String,
      },
      slug:{
        type: String,
        unique: true,
        index: 1,
        autoform: {
          omit: true
        },
        autoValue: function() {
          if (this.field('name').isSet) {
            return s.slugify( (this.field('name').value).toLowerCase() );
          }
        },
      },
      subcategories: {
        type: [Schemas.Subcategories],
        optional: true,
      },
    });
    
    Categories.attachSchema(Schemas.Categories);
    

    【讨论】:

      猜你喜欢
      • 2015-06-22
      • 2021-11-30
      • 1970-01-01
      • 2021-11-29
      • 2018-08-21
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多