2

我正在尝试使用 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);
}
});

提前致谢!:)

4

1 回答 1

4

我从未使用过 autoform,但在文档中它说它已经为您提供了“自动插入和更新事件,以及自动反应验证”。

所以应该没有必要指定你自己的事件处理程序。

文档中,您还将找到书籍示例。我只是从那里复制:

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>
于 2014-08-12T07:35:24.563 回答