Przeglądaj źródła

W4.9: working comments.

Frederic G. MARAND 8 lat temu
rodzic
commit
e4b0a89537
5 zmienionych plików z 34 dodań i 4 usunięć
  1. 13 0
      client/docItem.html
  2. 7 0
      client/main.js
  3. 3 0
      lib/collections.js
  4. 6 1
      server/main.js
  5. 5 3
      shared/main.js

+ 13 - 0
client/docItem.html

@@ -19,6 +19,8 @@
       </div>
     </div>
 
+    {{> commentList }}
+
     {{> insertCommentForm }}
   </div><!-- end of docItem container -->
 
@@ -55,6 +57,16 @@
   {{/each}}
 </template>
 
+<template name="commentList">
+  <ul>
+    {{#each comments }}
+      <li>{{ title }}
+        <p>{{ body }}</p>
+      </li>
+    {{/each}}
+  </ul>
+</template>
+
 <template name="insertCommentForm2">
   {{> quickForm collection="Comments" id="insertCommentForm" type="method" meteormethod="addComment" }}
 </template>
@@ -66,6 +78,7 @@
       {{> afQuickField name="title" }}
       {{> afQuickField name="body" rows=6 value="starter text" }}
       {{> afQuickField name="docid" value=docid type="hidden" }}
+      {{> afQuickField name="owner" value="invalid" type="hidden" }}
     </fieldset>
     <button type="submit" class="btn btn-primary">Insert</button>
   {{/autoForm}}

+ 7 - 0
client/main.js

@@ -5,6 +5,7 @@
 // subscribe to read data
 Meteor.subscribe("documents");
 Meteor.subscribe("editingUsers");
+Meteor.subscribe("comments");
 
 Router.configure({
   layoutTemplate: "ApplicationLayout"
@@ -91,6 +92,12 @@ Template.insertCommentForm.helpers({
   }
 });
 
+Template.commentList.helpers({
+  comments: function () {
+    return Comments.find({ docid: Session.get("docid") });
+  }
+});
+
 Template.docMeta.helpers({
   // Find current document.
   document: function () {

+ 3 - 0
lib/collections.js

@@ -19,5 +19,8 @@ Comments.attachSchema(new SimpleSchema({
   },
   docid: {
     type: String
+  },
+  owner: {
+    type: String
   }
 }));

+ 6 - 1
server/main.js

@@ -17,7 +17,7 @@ Meteor.publish("documents", function () {
   Meteor._debug("Publishing documents");
   return Documents.find({
     $or: [
-      { isPrivate: { $ne: true } }, 
+      { isPrivate: { $ne: true } },
       { owner: this.userId }
     ]
   });
@@ -28,3 +28,8 @@ Meteor.publish("editingUsers", function () {
   Meteor._debug("Publishing all editingUsers");
   return EditingUsers.find();
 });
+
+Meteor.publish("comments", function () {
+  Meteor._debug("Publishing comments");
+  return Comments.find();
+});

+ 5 - 3
shared/main.js

@@ -9,9 +9,11 @@ Meteor.methods({
     Meteor._debug("addComment method running", comment);
     // We are connected.
     if (this.userId) {
-      comment.createdOn = new Date();
-      comment.userId = this.userId;
-      return Comments.insert(comment);
+      //comment.createdOn = new Date();
+      comment.owner = this.userId;
+      const cid = Comments.insert(comment);
+      Meteor._debug("Comment id after insert", cid);
+      return cid;
     }
     return null;
   },