我应该如何使用autoform / collection2插入Meteor集合?

时间:2014-08-12 01:02:57

标签: meteor meteor-collection2

我正在尝试使用Meteor进行autoform books示例。我该怎么做Books.insert?

我看到了这个例子:

Books.insert({title: "Ulysses", author: "James Joyce"}, function(error, result) {
  //The insert will fail, error will be set,
  //and result will be undefined or false because "copies" is required.
  //
  //The list of errors is available on
  //`error.invalidKeys` or by calling
  Books.simpleSchema().namedContext().invalidKeys()
});

我不完全确定如何将其与我的其余代码联系起来:

if (Meteor.isClient) {

   Books = new Meteor.Collection("books");

   var Schemas = {};

  Schemas.Book = new SimpleSchema({

  title: {
   type: String,
   label: "Title",
   max: 200,
   optional: true
  },
  author: {
   type: String,
   label: "Author",
   optional: true
  },
  copies: {
   type: Number,
   label: "Number of copies",
   min: 0,
   optional: true
  },
  lastCheckedOut: {
    type: Date,
    label: "Last date this book was checked out",
    optional: true
  },
  summary: {
    type: String,
    label: "Brief summary",
    optional: true,
    max: 1000
  }
});

Books.attachSchema(Schemas.Book);

}

有人可以就此提出任何建议吗?

我在想我需要喜欢这个:

Template.bookform.events({
'click btn.submit': function () {
  var form = document.getElementById("formID").value;
  Books.insert(form);
}
});

提前致谢! :)

1 个答案:

答案 0 :(得分:4)

我从未使用过autoform,但是在documentation它说它已经为你提供了自动插入和更新事件,以及自动反应验证"。

因此不需要指定自己的事件处理程序。

docs中,您还可以找到图书示例。我只是从那里复制:

JS

Books = new Meteor.Collection("books", {
    schema: {
        title: {
            type: String,
            label: "Title",
            max: 200
        },
        author: {
            type: String,
            label: "Author"
        },
        copies: {
            type: Number,
            label: "Number of copies",
            min: 0
        },
        lastCheckedOut: {
            type: Date,
            label: "Last date this book was checked out",
            optional: true
        },
        summary: {
            type: String,
            label: "Brief summary",
            optional: true,
            max: 1000
        }
    }
});

if (Meteor.isClient) {
  Meteor.subscribe("books");
}

if (Meteor.isServer) {
  Meteor.publish("books", function () {
    return Books.find();
  });
}

HTML

<head>
  <title>Book example</title>
</head>

<body>
  {{> insertBookForm}}
</body>

<template name="insertBookForm">
  {{> quickForm collection="Books" id="insertBookForm" type="insert"}}
</template>