流星:如何制作两个视图 - 列表和文章

时间:2015-08-15 10:58:41

标签: javascript meteor

在meteor中创建模板以使用两个不同视图的最佳方法是什么?我们来打电话给模板'文章'默认情况下,应该有一个包含输入字段的所有文章的列表来创建/添加新元素。选择文章时,应显示所选文章。

所以我现在正在尝试的是:

package.js:

Package.onUse(function(api){
    api.versionsFrom("METEOR@1.1.0.3");
    api.addFiles([
        'lib/client/templates/article.html',
    ], ['client']);
});

router.js:

Router.route('/article/:_id?', {
  name: 'article'
})

模板/ article.html:

<template name="article">
    <form><input name="createNewArticle" type="text"></form>
    <ul><li>List of articles</li></ul>
</template>

<template name="article_detail">
    <p>Content of article</p>
</template>

1 个答案:

答案 0 :(得分:1)

如果您希望重定向到新页面,请确保您有两个单独的路线(一个用于所有文章的列表,另一个用于单个文章或文章详细信息)

<强> LIB / router.js

Router.route('/articles', {
  name: 'articles',
  waitOn: function () {
    return [
      Meteor.subscribe('articles'),
    ];
  },
  data: function() {
     return {
       articles: Articles.find({})
     }
  }
});

Router.route('/article/:_id', {
  name: 'article',
  template: 'article',
  waitOn: function () {
    return [
      Meteor.subscribe('article', this.params._id),
    ];
  },
  data: function () {
    return { 
       article: Articles.findOne({
        _id: this.params._id
      }) 
    };
  }
});

<强>的客户机/模板/ article.html:

<template name="articles">
    <form><input name="createNewArticle" type="text"></form>
    <ul>
      {{#each articles}}
        <a href="{{pathFor 'article'}}"><li>{{title}}</li></a>
      {{/each}}
    </ul>
</template>

<template name="article">
    <p>{{title}}</p>
</template>

确保您在server/publications.js

中设置了自己的出版物