簡體   English   中英

MeteorJS:如何獲取ID以從集合中加載

[英]MeteorJS: How to get id to load from collection

我正在嘗試加載一個數組(帶有簡單文本),並試圖在每次調用它時將其加載到模板上。 如何從特定項目中獲取ID,以獲取存儲在其中的數組?

HTML模板:

<template name="commentMarker">
    <div id="viewMarker">
        <h3 id="markerTitle">{{markerName}}</h3>
        <h6 id="markerCategory">{{markerCategory}}</h6>
        <br>
        <fieldset>
            <legend>Description</legend>
            <p>{{markerDescription}}</p>
        </fieldset>
        <form id="commentForm"> 
            <fieldset>
                <legend>Comments</legend>
                <input type="text" id="markerId" name="idForComment" value={{markerId}}>
                <textarea rows="3" cols="19" name="comment" id="commentArea" placeholder="Insert your comment here..."></textarea>
                {{#each comments}}
                    <p id="oneComment">{{this}}</p>
                {{/each}}
            </fieldset>
            <input type="submit" value="Comment" class="commentButton">
            <input type="submit" value="Close" class="exitButton">
        </form>
    </div>
</template>

JS:

Template.commentMarker.helpers({
comments(){
        alert(template.find("#markerId").value);
        if(commentArray.length===0) return;
        else return commentArray;
    }});

在這里,我將評論插入收藏集的項目中,並且工作正常

Template.commentMarker.events({
    'click .commentButton': function(e, template){
        e.preventDefault();
        var id = template.find("#markerId").value;
        var comment = template.find("#commentArea").value;
    Points.update(id, { $push: { comments: comment }});
        commentArray = Points.findOne(id).comments;
        template.find("#commentArea").value = ' ';
    }

我嘗試使用commentArray作為仍然是的全局變量。 但是我不知該如何從該特定項目中獲取ID,我什至將其ID(帶有隱藏顯示)放入表格中,以便實際上能夠插入注釋。 但這對顯示注釋沒有幫助,因為我似乎無法進入Template.helpers中的此字段...

不確定您要做什么。 就像在更新收藏集后立即顯示評論一樣。 看來您是完全在本地而非在線收藏上進行此操作。

但是,將其存儲為會話可以工作...或反應性var。 可能不是最好的解決方案。 基本上替換commentArray = Points.findOne(id).comments; 與:

Session.set('comments', Points.findOne(id).comments)

然后在助手中找到它:

let commentArray = Session.get('comments')

始終將其用於敏感數據並不安全。 還請嘗試捕獲findOne(id).comments,因為如果碰巧找不到它,它確實會產生錯誤。

注意:如果要使用Meteor.Methods,則不能使用Session。 您必須返回ID並在助手中找到它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM