Meteor,使用IronRouter附加一个带有集合的唯一页面

时间:2016-12-08 22:00:18

标签: javascript mongodb meteor iron-router

使用Meteor的IronRouter,如何附加集合的唯一ID并根据所述ID创建页面?此外,在页面中,如何从该集合中获取信息并显示它?

在集合中,我试图收集提交的ID,但是我很难理解如何从唯一页面上的集合中检索数据。

到目前为止,对于收藏,我有:

import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import { Works } from '../api/works.js';

import './work.js';
import './body.html';

Template.main.helpers({
  works() {
    return Works.find({}, { sort: { createdAt: -1 } });
  },
});

Template.main.events({
  'submit .new-work'(event) {
    event.preventDefault();

    const title = event.target.title.value;
    const workBriefDesc = event.target.workBriefDesc.value;
    const workFullDesc = event.target.workFullDesc.value;
    const workId = this._id;

    Works.insert({
      title,
      workBriefDesc,
      workFullDesc,
      createdAt: new Date(),
      owner: Meteor.userId(),
      username: Meteor.user().username,
      workId,
    });

     event.target.title.value = '';
     event.target.workbriefdesc.value = '';
     event.target.workfulldesc.value = '';

  },
});

对于IronRouter文件:

Router.route('/works/:_id', function () {
  this.render('Collab');
}, {
  name: 'collab',
    data: function(){
        var workId = this.params._id;
        return Works.findOne({ _id: workId });
    }
});

模板文件:

<!-- Publishing the template work -->
<template name="main">
                        <form class="new-work col s12">
                            <div class="row">
                                <div class="input-field col s6">
                                    <input id="title" type="text" class="validate">
                                    <label for="title">Name of work</label>
                                </div>
                                <div class="input-field col s6">
                                    <select>
                                        <option value="" selected>Choose category</option>
                                        <option value="1">Prose</option>
                                    </select>
                                    <label></label>
                                </div>
                            </div>
                            <div class="row">
                                <div class="input-field col s12">
                                    <input id="workBriefDesc" type="text" length="250">
                                    <label for="workBriefDesc">Brief description</label>
                                </div>
                            </div>
                            <div class="row">
                                <div class="input-field col s12">
                                    <textarea id="workFullDesc" class="materialize-textarea" length="10000"></textarea>
                                    <label for="workFullDesc">Full description</label>
                                </div>
                            </div>
                            <div class="row">
                                <div class="input-field col s12">
                                    <textarea id="workessay" class="materialize-textarea"></textarea>
                                    <label for="workessay">Essay</label>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button href="#!" class="modal-action modal-close waves-effect waves-grey btn-flat center" type="submit" name="action">Submit</button>
                            </div>
                        </form>
    {{#each works}} {{ > work}} {{/each}}
</template>

<!-- Link to the unique page -->
<template name="work">
    Go to <a href="/work/{{_id}}">work</a>
</template>


<!-- Unique page attached to ID -->
<template name="collab">
    {{title}} <br>
    {{workBriefDesc}} <br>
    {{workFullDesc}}
</template>

2 个答案:

答案 0 :(得分:0)

你必须修改你的助手:

Template.main.helpers({
  works: function(){
    return Works.findOne({_id:Router.current().params.workId});
  },
});

答案 1 :(得分:0)

帮助者:我认为,你应该在你的工作中加上一个条件&#34;帮手。

您的代码

Template.main.helpers({
  works() {
    return Works.find({}, { sort: { createdAt: -1 } });
  },
});

在上面的代码中,请更改return语句并尝试。

<强>更新

return Works.findOne({_id:Router.current().params._Id});

希望这项工作!