Ver Fonte

W3.6: add poster name to image.

Frederic G. MARAND há 9 anos atrás
pai
commit
9292c03eff
2 ficheiros alterados com 14 adições e 6 exclusões
  1. 1 0
      image_share.html
  2. 13 6
      image_share.js

+ 1 - 0
image_share.html

@@ -57,6 +57,7 @@
           <div class="caption">
             <h3>Rating: {{ rating }}</h3>
             <p>{{ img_alt }}</p>
+            <p>User: {{ getUser createdBy }}</p>
             <p>{{> starsRating mutable=true class="js-rate-image" id=image_id }}</p>
             <button class="js-del-image btn btn-warning">delete</button>
           </div>

+ 13 - 6
image_share.js

@@ -10,6 +10,10 @@ if (Meteor.isClient) {
     images: Images.find({}, { sort: { createdOn: -1, rating: -1 }}),
     image_id: function () {
       return "rating-" + this._id;
+    },
+    getUser: function (userId) {
+      let user = Meteor.users.findOne({ _id: userId });
+      return user ? user.username : "anonymous";
     }
   });
 
@@ -55,12 +59,15 @@ if (Meteor.isClient) {
       let img_alt = event.target.img_alt.value;
       console.log("src: " + img_src + " alt: " + img_alt);
 
-      Images.insert({
-        img_src: img_src,
-        img_alt: img_alt,
-        createdOn: new Date()
-      });
-       $("#image_add_form").modal('show');
+      if (Meteor.user()) {
+        Images.insert({
+          img_src: img_src,
+          img_alt: img_alt,
+          createdOn: new Date(),
+          createdBy: Meteor.userId()
+        });
+      }
+      $("#image_add_form").modal("hide");
       return false;
     }
   });