Przeglądaj źródła

W4.8: basic use of autoform, collection2, simple-schema.

Frederic G. MARAND 8 lat temu
rodzic
commit
72e8634a62
7 zmienionych plików z 65 dodań i 2 usunięć
  1. 5 1
      .eslintrc.js
  2. 2 0
      .meteor/packages
  3. 4 0
      .meteor/versions
  4. 20 1
      client/docItem.html
  5. 6 0
      client/main.js
  6. 17 0
      lib/collections.js
  7. 11 0
      shared/main.js

+ 5 - 1
.eslintrc.js

@@ -25,9 +25,13 @@ module.exports = {
     // IronRouter
     "Router": true,
 
+    // aldeed:autoform
+    SimpleSchema: true,
+
     // Project-specific.
     "Documents": true,
-    "EditingUsers": true
+    "EditingUsers": true,
+    "Comments": true
   },
 
   "plugins": ["react"],

+ 2 - 0
.meteor/packages

@@ -25,3 +25,5 @@ accounts-password
 ian:accounts-ui-bootstrap-3
 babrahams:editable-text
 iron:router
+aldeed:autoform
+aldeed:collection2

+ 4 - 0
.meteor/versions

@@ -1,5 +1,8 @@
 accounts-base@1.2.2
 accounts-password@1.1.4
+aldeed:autoform@5.8.1
+aldeed:collection2@2.3.1
+aldeed:simple-schema@1.1.0
 anti:i18n@0.4.3
 autoupdate@1.2.4
 babel-compiler@5.8.24_1
@@ -61,6 +64,7 @@ mizzao:sharejs@0.7.5
 mizzao:sharejs-codemirror@4.12.0
 mobile-experience@1.0.1
 mobile-status-bar@1.0.6
+momentjs:moment@2.10.6
 mongo@1.1.3
 mongo-id@1.0.1
 mongo-livedata@1.0.9

+ 20 - 1
client/docItem.html

@@ -18,7 +18,10 @@
         {{> viewer }}
       </div>
     </div>
-  </div>
+
+    {{> insertCommentForm }}
+  </div><!-- end of docItem container -->
+
 </template>
 
 <template name="editor">
@@ -51,3 +54,19 @@
 
   {{/each}}
 </template>
+
+<template name="insertCommentForm2">
+  {{> quickForm collection="Comments" id="insertCommentForm" type="method" meteormethod="addComment" }}
+</template>
+
+<template name="insertCommentForm">
+  {{#autoForm collection="Comments" id="insertCommentForm" type="method" meteormethod="addComment" }}
+    <fieldset>
+      <legend>Comment</legend>
+      {{> afQuickField name="title" }}
+      {{> afQuickField name="body" rows=6 value="starter text" }}
+      {{> afQuickField name="docid" value=docid type="hidden" }}
+    </fieldset>
+    <button type="submit" class="btn btn-primary">Insert</button>
+  {{/autoForm}}
+</template>

+ 6 - 0
client/main.js

@@ -85,6 +85,12 @@ Template.docList.helpers({
   }
 });
 
+Template.insertCommentForm.helpers({
+  docid: function () {
+    return Session.get("docid");
+  }
+});
+
 Template.docMeta.helpers({
   // Find current document.
   document: function () {

+ 17 - 0
lib/collections.js

@@ -4,3 +4,20 @@ this.Documents = new Mongo.Collection("documents");
 
 // This collection stores sets of users that are editing documents.
 EditingUsers = new Mongo.Collection("editingUsers");
+
+Comments = new Mongo.Collection("comments");
+Comments.attachSchema(new SimpleSchema({
+  title: {
+    type: String,
+    label: "Title",
+    max: 200
+  },
+  body: {
+    type: String,
+    label: "Comment",
+    max: 1000
+  },
+  docid: {
+    type: String
+  }
+}));

+ 11 - 0
shared/main.js

@@ -5,6 +5,17 @@
 
 // Methods that provide write access to the data.
 Meteor.methods({
+  addComment: function (comment) {
+    Meteor._debug("addComment method running", comment);
+    // We are connected.
+    if (this.userId) {
+      comment.createdOn = new Date();
+      comment.userId = this.userId;
+      return Comments.insert(comment);
+    }
+    return null;
+  },
+
   addDoc: function () {
     Meteor._debug("addDoc, this", this);
     // Not logged-in.