Javascript 使用Iron Router设置查询变量
我正在尝试使用Iron Router调用页面并按路由过滤结果。实际上,应用程序允许您创建项目。项目包括一个可以有0到多个标记的数组:Javascript 使用Iron Router设置查询变量,javascript,mongodb,meteor,iron-router,Javascript,Mongodb,Meteor,Iron Router,我正在尝试使用Iron Router调用页面并按路由过滤结果。实际上,应用程序允许您创建项目。项目包括一个可以有0到多个标记的数组: Item: { _id: <assigned by application>, itemTags: [] } 项目:{ _身份证: {{#每个唯一标签} {{/每个}} //JS Template.header.helpers({ uniqueTags:function(){ var allItems=Items.find({c
Item: {
_id: <assigned by application>,
itemTags: []
}
项目:{
_身份证:
{{#每个唯一标签}
{{/每个}}
//JS
Template.header.helpers({
uniqueTags:function(){
var allItems=Items.find({checked:false});
var uniqueTags=allItems.map((函数(allItems){return allItems.itemTags;}));
uniqueTags=u0.flatte(uniqueTags);
uniqueTags=u0.compact(uniqueTags);
uniqueTags=uq.uniq(uniqueTags);
uniqueTags=uu.sortBy(uniqueTags,函数(名称){returnname;});
返回唯一标签;
}
});
路由器然后将您发送到“/list/”。到目前为止,一切都很好。但随后它就崩溃了。我的问题有两个:
1) 如何恰当地获取数组中某个位置列出了标记的项的数据上下文
2) 如何在“列表”页面上显示这些项目?我不知道list.js中应该有什么来处理返回的数据上下文
我当前的代码如下,但它已被黑客攻击,显然无法正常工作:)
//JS-router.JS
Router.route(“/list/:标记名”{
名称:'列表',
数据:函数(){
返回项目。查找({$and:[
{checked:false},{listItem:true},{itemTags:{$in:['this.params.tagName']}
]});
}
});
//HTML-list.HTML
列表
{{#每个列表项}
{{>项目}
{{/每个}}
//JS-list.JS
Template.list.helpers({
listItems:function(){
}
});
谢谢你的帮助!这会有用的
Router.route('/list/:tagName', {
name: 'list',
data: function() {
return { 'listItems' : Items.find({$and: [
{checked: false}, {listItem: true}, {itemTags: {$in: [this.params.tagName]}}
]}) };
}
});
// HTML - list.html
<template name="list">
<h3><span class="label label-default view-list">List</span></h3>
{{#each listItems}}
{{> item}}
{{/each}}
</template>
// You have to write a item template
// JS - list.js
Template.list.helpers({
//listItems: function() {
// this helper is not required
// you could access the data by this.data.listItems
//<somehow use the returned data context>
//}
});
Router.route(“/list/:标记名”{
名称:'列表',
数据:函数(){
返回{'listItems':Items.find({$and:[
{checked:false},{listItem:true},{itemTags:{$in:[this.params.tagName]}
]}) };
}
});
//HTML-list.HTML
列表
{{#每个列表项}
{{>项目}
{{/每个}}
//您必须编写一个项目模板
//JS-list.JS
Template.list.helpers({
//listItems:function(){
//此助手不是必需的
//您可以通过this.data.listItems访问数据
//
//}
});
为什么要处理helpers(list.js)中的数据,您可以在路由器本身中处理它,然后使用手柄将其放置在模板中,您可以使用属性数据创建一个对象data
。listItems
填充来自路由器的listItems
,然后您不需要助手,然后list.html
就可以了。我想我不需要。我只是不知道怎么做在路由器中处理多个返回的项目并用空格键放置它们。编写路由器和模板以便将所有返回的项目传递到{{{each}}的正确方法是什么?您可以编写someObject.listItems=items.find({yourQuery}).fetch();
然后将其从路由器返回到list.html
模板,然后可以像{{tagText}一样访问每个列表项的属性
不管它是什么。从路由器返回的数据中的listItem
属性将类似于helper数据。是否要在itemTags
模板中的项目数组中列出标记,因为每个项目都与路由器中的查询相匹配?您的解决方案非常接近。我必须删除lis中的单个引号t在return
语句中以及从大约这个.params.tagName
开始,它就工作了。感谢所有的帮助!我以为你对查询很确定,并且对将其放置到Template有疑问。我很擅长查询以找到标记并将它们放在下拉列表中。并且对查找项目有基本的想法。但是销毁这些物品是一个很大的障碍,关键是返回spacebars参考。
// JS - router.js
Router.route('/list/:tagName', {
name: 'list',
data: function() {
return Items.find({$and: [
{checked: false}, {listItem: true}, {itemTags: {$in: ['this.params.tagName']}}
]});
}
});
// HTML - list.html
<template name="list">
<h3><span class="label label-default view-list">List</span></h3>
{{#each listItems}}
{{> item}}
{{/each}}
</template>
// JS - list.js
Template.list.helpers({
listItems: function() {
<somehow use the returned data context>
}
});
Router.route('/list/:tagName', {
name: 'list',
data: function() {
return { 'listItems' : Items.find({$and: [
{checked: false}, {listItem: true}, {itemTags: {$in: [this.params.tagName]}}
]}) };
}
});
// HTML - list.html
<template name="list">
<h3><span class="label label-default view-list">List</span></h3>
{{#each listItems}}
{{> item}}
{{/each}}
</template>
// You have to write a item template
// JS - list.js
Template.list.helpers({
//listItems: function() {
// this helper is not required
// you could access the data by this.data.listItems
//<somehow use the returned data context>
//}
});