ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript – Meteor AutoForm:如何使用子文档数组更新架构值?

2019-06-28 10:24:21  阅读:206  来源: 互联网

标签:javascript meteor meteor-autoform simple-schema


我是Meteor AutoForm的新手.我想用国家/地区doc更新播放器文档.以下是我的代码.如果我添加{{> afQuickField name =“country”}}在AutoForm中它不起作用. {{&GT afQuickField name =“name”}}单独工作正常.如何在玩家国家/地区字段中添加整个国家/地区文档(_id,slug,name)?

JS:

CountrySchema = new SimpleSchema({
    _id: {
        type: String,
        index: true
    },
    slug: {
        type: String,
        max: 100,
        index: true
    },
    name: {
        type: String,
        max: 100
    }
});

Player.attachSchema(new SimpleSchema({
    name: {
        type: String,
        label: "Player name",
        index: true
    },
    country: {
        type: [CountrySchema],
        label: "country",
        max: 5,
        index: true,
        autoform: {
            options: function() {
                return Country.find().fetch().map(function(object) {
                    return {
                        label: object.name,
                        value: object._id
                    };
                });
            }
        }
    }
})); 

HTML:

{{#autoForm id="editplayer" }}
   {{> afQuickField name="name"}}
   {{> afQuickField name="country"}}
{{/autoForm}}

我添加了SimpleSchema.debug = true;控制台日志显示“editplayer”上下文的SimpleSchema无效键.

解决方法:

如果要将country作为数组字段呈现,可以使用afArrayField组件:

<template name="editPlayer">
    {{#autoForm id="editplayer" collection="Player" type="update" doc=currentPlayer}}
        {{> afQuickField name="name"}}
        {{> afArrayField name="country"}}
        <button type="submit">Submit</button>
    {{/autoForm}}
</template>
if (Meteor.isClient) {
    AutoForm.debug();
    Template.editPlayer.helpers({
        currentPlayer: function () {
            return Player.findOne();
        }
    });
}

if (Meteor.isServer) {
    Meteor.startup(function () {
        let country = {slug: 'austria', name: 'Austria'};
        Country.insert(country);
        Player.insert({name: 'Matthias Eckhart', country: [country]});
    });
}

Player = new Mongo.Collection("player");
Country = new Mongo.Collection("country");

CountrySchema = new SimpleSchema({
    slug: {
        type: String,
        max: 100,
        index: true
    },
    name: {
        type: String,
        max: 100
    }
});

Player.attachSchema(new SimpleSchema({
    name: {
        type: String,
        label: "Player name",
        index: true
    },
    country: {
        type: [CountrySchema],
        label: "country",
        max: 5,
        index: true,
        autoform: {
            options: function () {
                return Country.find().map((object) => ({label: object.name, value: object._id}));
            }
        }
    }
}));

请注意,我假设您要更新播放器文档.因此,我设置属性doc = currentPlayer并通过currentPlayer辅助函数指定播放器文档.如果设置数据上下文,例如通过Iron Router路径中的data function,则可以使用doc = this.

如果您想要一个简单的选择表单,您当前的数据结构可能需要一个解决方法.问题是[CountrySchema]没有扩展到正确的架构键.因此,您需要使用AutoForm hooks和Meteor辅助函数(currentPlayer)显式指定完整模式并转换所选选项:

<template name="editPlayer">
    {{#autoForm id="editplayer"  collection="Player" type="update" doc=currentPlayer}}
        {{> afQuickField name="name"}}
        {{> afQuickField name="country"}}
        <button type="submit">Submit</button>
    {{/autoForm}}
</template>
if (Meteor.isClient) {
    AutoForm.debug();
    Template.editPlayer.helpers({
        currentPlayer: function () {
            let player = Player.findOne();
            if (player) player.country = _.map(player.country, (country) => country._id);
            return player;
        }
    });
    var playerUpdateHook = {
        before: {
            update: function (doc) {
                doc['$set'].country = _.map(doc['$set'].country, (countryId) => Country.findOne({_id: countryId}));
                return doc;
            }
        }
    };
    AutoForm.addHooks('editplayer', playerUpdateHook);
}

if (Meteor.isServer) {
    Meteor.startup(function () {
        let country = {slug: 'austria', name: 'Austria'};
        let countryId = Country.insert(country);
        country = Country.findOne({_id: countryId});
        Player.insert({name: 'Matthias Eckhart', country: [country]});
    });
}

Player = new Mongo.Collection("player");
Country = new Mongo.Collection("country");

CountrySchema = new SimpleSchema({
    _id: {
        type: String,
        index: true
    },
    slug: {
        type: String,
        max: 100,
        index: true
    },
    name: {
        type: String,
        max: 100
    }
});

Player.attachSchema(new SimpleSchema({
    name: {
        type: String,
        label: "Player name",
        index: true
    },
    country: {
        type: [CountrySchema],
        label: "country",
        max: 5,
        index: true
    },
    'country.$': {
        type: String,
        autoform: {
            options: function () {
                return Country.find().map((object) => ({label: object.name, value: object._id}));
            }
        }
    }
}));

标签:javascript,meteor,meteor-autoform,simple-schema
来源: https://codeday.me/bug/20190628/1314681.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有