选择类别而不更新子类别 [英] Select category not updating sub category

查看:173
本文介绍了选择类别而不更新子类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是为了查看用户类别选择,然后更新子类别.该解决方案是由其他人推荐的,但我似乎无法使其正常工作.当我选择类别时,子类别不会更新.有人可以让我知道我在想什么.

This is suppose to look at the users category selection and then update the subcategory. The solution was recommended by someone else but I can't seem to get it to work. When I select the category, subcategory doesn't update. Can someone let me know what I'm missing.

路径:category.js

<template name="category">
    {{#autoForm collection="Meteor.users" id="categoryForm" doc=currentUser type="update"}}
        {{> afQuickField name='profile.categories'}}
    {{/autoForm}}   
</template>

路径:Schema.js

var fruitArr = ['apple', 'banana'];
var vegetablesArr = ['carrot', 'broccoli'];

Schema.Category = new SimpleSchema({
    category: {
        type: String,
        label: "Category",
        allowedValues: ['fruit', 'vegetables']
    },
    subcategory: {
        type: String,
        label: "Subcategory",
        allowedValues: _.union(fruitArr, vegetablesArr),
        autoform: {
            options: function () {
                let category = AutoForm.getFieldValue("category");
                if (!category) return [{label: "Please select a category first", value: ""}];

                if (category === "fruit") return _.map(fruitArr, (v, i) => ({
                    label: "Fruit " + (i + 1) + ": " + v,
                    value: v
                }));
                else return _.map(vegetablesArr, (v, i) => ({label: "Vegetables " + (i + 1) + ": " + v, value: v}));
            }
        }
    }
});

Schema.UserProfile = new SimpleSchema({
    categories: {
        type: Schema.Category,
        optional: true,
    }
});

推荐答案

在浏览器的控制台日志中调用AutoForm.getFormValues('categoryForm');时,将返回以下结果:

When calling AutoForm.getFormValues('categoryForm'); in the browser's console log, the following result will be returned:

{
   "insertDoc":{
      "profile":{
         "categories":{
            "category":"fruit"
         }
      }
   },
   "updateDoc":{
      "$set":{
         "profile.categories.category":"fruit"
      },
      "$unset":{
         "profile.categories.subcategory":""
      }
   }
}

从上面可以看到,模式字段subcategory被引用为profile.categories.subcategory.因此,字段subcategory不会更新,因为AutoForm.getFieldValue("category");返回undefined.

As you can see from above, the schema field subcategory is referenced as profile.categories.subcategory. Therefore, the field subcategory won't be updated because AutoForm.getFieldValue("category"); returns undefined.

您可以通过更改来解决此错误

You can fix this error by changing

let category = AutoForm.getFieldValue("category"); 

let category = AutoForm.getFieldValue("profile.categories.category");

subcategory模式字段中的options函数内部.

inside your options function in the subcategory schema field.

这篇关于选择类别而不更新子类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆