Meteor: show each element from an array from mongo in a separate list tag

Note. The whole code can be found here:

https://github.com/Julian-Th/crowducate-platform/tree/feature/courseEditRights

Currently, all elements from the array are displayed in one list instead of a separate list tag:

enter image description here

My JS (I commented on some previous approaches):

Template.modalAddCollaborators.events({
    'click #js-addCollaborator' : function (event) {
        var collaboratorName = $('#collaboratorName').val(); // 
        Courses.update(
           { _id: this._id },
           { $addToSet: {canEditCourse: collaboratorName } }
        )
        $('#collaboratorName').val("");
    }
});

Template.modalAddCollaborators.helpers({
    'addedCollaborators': function () {
        return Courses.find();
        //return Courses.find({_id: this._id}, {$in: "canEditCourse"});
        //return Courses.distinct("canEditCourse");
    }
});

My HTML:

<template name="modalAddCollaborators">
  <div id="modalAddCollaborators" class="modal fade" role="dialog">
    <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Manage Your Collaborators</h4>
      </div>
      <div class="modal-body">
        <form class="form" role="form">
          <ul class="list-group">
            {{#each  addedCollaborators}}<li class="list-group-item">{{canEditCourse}}</li>{{/each}}
          </ul>
          <div class="form-group">
            <input type="text" id="collaboratorName" placeholder="add a collaborator  ...">
            <button type="button" id="js-addCollaborator" class="btn btn-success">Add</button>
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>
</template>

MY JSON:

{
    "_id" : "rmZEFmfoBwf4NwqX4",
    "title" : "Love",
    "coverImageId" : "P7PyR6x64uCSX7X9m",
    "author" : "test",
    "keywords" : [ 
        "test"
    ],
    "published" : "true",
    "about" : "test",
    "canEditCourse" : [ 
        "wicazRk3EsThE5E8W", 
        "Jolle", 
        "jolle", 
        "vW59A6szZijMDLDNh"
    ],
    "createdById" : "wicazRk3EsThE5E8W",
    "dateCreated" : ISODate("2015-12-27T15:06:28.272Z")
}

Any help is appreciated, thanks.

+4
source share
2 answers

Courses.find();returns a cursor, not an array. Use instead : fetch()

Template.modalAddCollaborators.helpers({
    'addedCollaborators': function () {
        return Courses.find().fetch();        
    }
});

{{#each}} , courses canEditCourse . this, , , :

<template name="modalAddCollaborators">
    {{#each addedCollaborators}}
        <h1>{{title}}</h1>
        <ul class="list-group">
        {{#each canEditCourse}}
            <li class="list-group-item">{{this}}</li>
        {{/each}}
        </ul>
    {{/each}}
</template>
+4

, canEditCourse:

  • String - Meteor.userId
  • String -

, userId, , , , .

UserID

canEditCourse,

Courses.helpers({
    "getCollaboratorUsernames": function () {
        // Get collaborator IDs array
        var userIds = this.canEditCourse;

        // Get the users, using MongoDB '$in' operator
        // https://docs.mongodb.org/v3.0/reference/operator/query/in/
        var users = Meteor.users.find({_id: {$in: userIds}).fetch();

        // placeholder array for usernames
        var collaboratorUsernames = []

        // Get username for each user, add it to usernames array
        users.forEach(function (user) {
            // Add current username to usernames array
            collaboratorUsernames.push(user.profile.username);
        });

        return collaboratorUsernames;
    }
});

, , userIds, (Courses.find().fetch()).

typeahead , Crowducate.

. , / Selectize.

Boodstrap. collaboratorUsernames :

{{# each getCollaboratorUsernames }}
    <span class="label label-info">{{ this }}</span>
{{/ each }}

. , - /:

:

Meteor.publish('courseCollaborators', function (courseId) {
    // Get the course object
    var course = Courses.findOne(courseId);

    // Get course collaborator IDs
    var collaboratorIds = course.canEditCourse;
    // Consider renaming the 'canEditCourse' field to 'collaboratorIds'
    // Then, it would look like
    // var courseCollaboratorIds = course.collaboratorIds;
    // Or, you could even skip that, and the code would still be literate

    // Get course collaborators
    var collaborators = Meteor.users.find({_id: {$in: collaboratorIds}).fetch();

    return collaborators;
});

template.created:

Template.modalAddCollaborators.created = function () {
    // Get reference to template instance
    var instance = this;

    // Get reference to router
    var route = Router.current();

    // Get course ID from route
    var courseId = route.params._id;

    // Subscribe to Course Collaborators, template level
    instance.subscribe("courseCollaborators", courseId);
};

Selectize if (instance.subscriptionsReady()) {}:

Template.modalAddCollaborators.rendered = function () {
    // Get reference to template instance
    var instance = this;

    // Make sure subscriptions are ready before rendering Selectize 
    if (instance.subscriptionsReady()) {
        // Get course collaborator usernames/IDs
        // Render the Selectize widget
            // User should see usernames
            // UserID is saved to collection
    }
};
+1

Source: https://habr.com/ru/post/1621965/


All Articles