【发布时间】:2015-09-21 11:55:04
【问题描述】:
MeteorJS 新手。我开始制作一个新颖的 Clan/Samurai 应用程序,看看我是否能理解 mongo/meteor 和 Autoforms 是如何处理关系的。我试图让氏族和武士相互关联,以便每个武士都有一个特定的氏族。
我正在尝试将氏族数据插入武士身份。我好像不能。
我已阅读以下内容,但似乎仍然对如何实现这一点感到困惑。我之前尝试过,onsuccess,onsubmit 钩子。我试图设置架构以使其正常工作。我主要得到 AutoForm undefined 或 Schema undefined ......似乎有很多错误。我读过它应该是客户端。
我可以控制日志并让它呈现,但我无法将新项目添加到集合中。
随机 Github 为观看乐趣而制作
https://github.com/qtheninja/SamuraiAttack
https://github.com/aldeed/meteor-collection2/issues/31 How to add a relationship or reference with AutoForm in Meteor? Meteor Autoform package with collection2 does not submit the form
//lib/collections/clans.js
Clans = new Mongo.Collection('clans');
Clans.attachSchema(new SimpleSchema({
name: {
type: String,
label: "Clan Name",
max: 100
}
}));
if (Meteor.isServer) {
Clans.allow({
insert: function (userId, doc) {
return true;
},
update: function (userId, doc, fieldNames, modifier) {
return true;
},
remove: function (userId, doc) {
return true;
}
});
}
//lib/collections/samurais.js
Samurais = new Mongo.Collection('samurais');
Samurais.attachSchema(new SimpleSchema({
title: {
type: String,
label: "Title",
max: 100
},
description: {
type: String,
label: "Description",
optional: true
},
clan: {
type: Clans
}
}));
if (Meteor.isServer) {
Samurais.allow({
insert: function (userId, doc) {
return true;
},
update: function (userId, doc, fieldNames, modifier) {
return true;
},
remove: function (userId, doc) {
return true;
}
});
}
//client/template/clans/createClan.html
<template name="CreateClan">
<h1>Create New Clan</h1>
{{> quickForm
collection="Clans"
id="insertClanForm"
type="insert"
buttonContent="Create"
}}
<div>
{{> ListClans }}
</div>
</template>
//client/main.js
AutoForm.addHooks('insertSamuraiForm', {
before: {
insert: function(doc, template) {
//modify the document here
doc.clanid= 45;
doc.dance ="start again";
console.log("running after hook");
return true;
}
}
});
AutoForm.hooks({
insertSamuraiForm: {
before: {
insert: function(doc, template) {
//modify the document here
doc.projectid= "random";
doc.dance ="start again";
console.log('this is asecond form');
}
}
}
});
【问题讨论】: