Javascript Meteor:如何创建两个视图-列表和文章
在meteor中,为使用两个不同视图创建模板的最佳方法是什么?让我们将模板称为“article”,默认情况下,应该有一个包含所有文章的列表,其中包含一个用于创建/添加新元素的输入字段。选择文章时,应显示所选文章 所以现在我想说的是: package.js:Javascript Meteor:如何创建两个视图-列表和文章,javascript,meteor,Javascript,Meteor,在meteor中,为使用两个不同视图创建模板的最佳方法是什么?让我们将模板称为“article”,默认情况下,应该有一个包含所有文章的列表,其中包含一个用于创建/添加新元素的输入字段。选择文章时,应显示所选文章 所以现在我想说的是: package.js: Package.onUse(function(api){ api.versionsFrom("METEOR@1.1.0.3"); api.addFiles([ 'lib/client/templates/art
Package.onUse(function(api){
api.versionsFrom("METEOR@1.1.0.3");
api.addFiles([
'lib/client/templates/article.html',
], ['client']);
});
Router.route('/article/:_id?', {
name: 'article'
})
<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>
<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>
router.js:
Package.onUse(function(api){
api.versionsFrom("METEOR@1.1.0.3");
api.addFiles([
'lib/client/templates/article.html',
], ['client']);
});
Router.route('/article/:_id?', {
name: 'article'
})
<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>
<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>
模板/article.html:
Package.onUse(function(api){
api.versionsFrom("METEOR@1.1.0.3");
api.addFiles([
'lib/client/templates/article.html',
], ['client']);
});
Router.route('/article/:_id?', {
name: 'article'
})
<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>
<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>
- 文章列表
文章内容
如果要重定向到新页面,请确保有两个单独的路由(一个用于所有文章列表,另一个用于单个文章或文章详细信息)
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
})
};
}
});
client/templates/article.html:
Package.onUse(function(api){
api.versionsFrom("METEOR@1.1.0.3");
api.addFiles([
'lib/client/templates/article.html',
], ['client']);
});
Router.route('/article/:_id?', {
name: 'article'
})
<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>
<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>
{{#每篇文章}
{{/每个}}
{{title}}
确保您也在
server/publications.js
中设置了出版物。选择文章时,应显示选定的文章。-您希望所选文章显示在同一页面上,如:或您希望重定向到新页面?重定向到新页面。我知道这很简单也很基本,但我不知道如何最好地完成这件简单的事情。是否可以使用“/article”来显示列表,使用“/article/:\u id”来显示详细的文章?比如检查是否有身份证……是的。把“/articles”改成“/articles”谢谢。仅仅是最后一个问题——甚至有点离题:如何保存输入框给定的新元素以创建新文章?我不知道您的代码是什么样子,但它应该是:Template.articles.events({'submit form':function(){articles.insert({title:$('#myfieldId');})代码>