【问题标题】:Meteor + autoform example requestedMeteor + autoform 示例请求
【发布时间】:2015-02-19 09:36:58
【问题描述】:

autoform docs 中,有很多sn-ps 示例,但我无法让它们中的任何一个工作。主要是因为 autoform、meteor 和最后的 JS 对我来说都是新的。

但是,我擅长改编示例,但找不到任何简单的示例。 This is one 我很挣扎。我可以获得一个使用集合的简单自动表单(或快速表单)的完整示例吗?

  1. 假设我已经安装了 aldeed:autoform 和 aldeed:collection2。
  2. 假设我的文件分为以下几部分

    • both/testform.js
    • 服务器/testform.js
    • client/testform.js
    • 客户端/testform.js?
  3. 假设我正在使用一个名为“testTemplate”的模板和一个名为“testCollection”的集合

感谢您的帮助。

【问题讨论】:

  • 当你尝试让它工作时,你在哪里卡住了?

标签: javascript node.js meteor


【解决方案1】:

我会尽量简化。

首先创建项目并删除autopublish and insecure

第二个/server/testform.js放这个。

TestCollection.allow({
  insert:function(){return true;},
  remove:function(){return true;},
  update:function(){return true;},
})

publish 函数

Meteor.publish("TestCollection", function () {
  return TestCollection.find();
});

更多关于allow/deny规则

根据 Meteor 最佳实践,将集合声明放在 /lib/testform.js 中,而不是 /both/testform.js,以确保首先对其进行评估。

TestCollection = new Mongo.Collection("TestCollection");

还有subscription

if(Meteor.isClient){
     Meteor.subscribe('TestCollection')
}

现在/client/testform.html

放这个。

<template name="testForm">
  {{> quickForm collection="TestCollection" id="insertTestForm" type="insert"}} 
</template>

现在在/client/testform.js 上放置架构

TestCollection.attachSchema(new SimpleSchema({ //take this from docs.
  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
  }
})); 

注意

如果您是 Meteor/Javascript 的新手,请不要跳入像这样的复杂包。

运行它,看看它们是如何工作的。

meteor create --example todos 
meteor create --example local market

或查看the meteor tutorial

对于 Javascript,本教程/指南对我有很大帮助 How to Learn Javascript properly

【讨论】:

  • 控制台在client/testform.js中报告“Uncaught ReferenceError: TestCollection is not defined”。如果我添加 TestCollection = new Mongo.Collection("TestCollection");在该文件的顶部,我收到另一个错误“未捕获的错误:collection.js 中已经有一个名为'TestCollection'的集合”(但表单呈现)。如果我在 both/testform.js 中注释掉 TestCollection 行,我会收到一个流星错误“ReferenceError: TestCollection is not defined”。有什么建议吗?
  • 好的,问题已解决并建议在上面进行编辑。如果集合定义按照您对此SO question 的回答在lib/collections.js 中完成,则更好。加载订单等等。
  • 感谢您的帮助。我确实做了一些实验。在使用其他一些复杂的软件包时,我并不是全新的,但足够新,以至于流星及其软件包的特性会让我感到困惑。感谢您对软件包的帮助和体验。
  • 您能否批准我对您的回答所做的修改。就目前而言,它不起作用。
  • 不确定当我建议修改您的答案时您是否会收到通知,但让我再试一次。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-22
  • 2015-09-20
  • 2016-08-14
  • 1970-01-01
  • 2016-12-04
  • 1970-01-01
相关资源
最近更新 更多