1

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

例如:

子类别.$.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() );
      }
    },
  },

谢谢,

4

1 回答 1

0

我通过更改架构得到了预期的结果。除了变得更有条理之外,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-11-28T09:39:18.007 回答