1
在我的应用程序中,我有一个代表投资组合中每个项目的图块列表。这是应用程序的主要列表视图,所有项目都是从集合中提取的,没有任何排序或排序。流星。通过深度嵌套值排序我的集合
当我在路由中指定了一个可选的slug参数(对于分配给项目的类别)时,我希望能够在界面中显示与该类别匹配的项目,然后显示其他类型的项目不符合类别。
作为参考,我已包括下面的路由的代码:
/**
* Project list view (all projects) with optional
* filter parameter for showing projects only by
* their category name.
*/
this.route('list', {
path: '/:_category_slug?',
template: 'template_main',
action: function() {
if(this.ready()) {
this.render();
}
},
waitOn: function() {
return [
Meteor.subscribe('projects'),
Meteor.subscribe('formations'),
Meteor.subscribe('categories')
];
},
data: function() {
if(this.params._category_slug) {
/**
* Building up the query given the category slug and the language
*/
var query = {};
query['slug.' + App.language] = this.params._category_slug;
/**
* Grab the category given the query, so we can get its 'id'
*/
var category = App.models.categories.findOne(query);
/**
* This is the query I need to work on so that I can achieve what I want
*/
return App.models.projects.find({}).fetch();
}
else {
return App.models.projects.find({}).fetch();
}
},
yieldTemplates: {
'components_header': {to: 'header'},
'views_list': {to: 'content'},
'components_footer': {to: 'footer'}
}
});
作为参考,我也包括该数据的样本为三个项目是相关的这个问题。
{
"id": 10,
"slug": {
"en": "sample-english-slug",
},
"title": {
"en": "Sample English Title",
},
"description": {
"en": "A good description.",
},
"category_ids": [
{
"id": 5
},
{
"id": 6
}
],
},
{
"id": 12,
"slug": {
"en": "another-sample-slug",
},
"title": {
"en": "Another sample title",
},
"description": {
"en": "Sample description three",
},
"category_ids": [
{
"id": 1
},
{
"id": 4
}
],
},
{
"id": 11,
"slug": {
"en": "another-sample-slug",
},
"title": {
"en": "A sample title",
},
"description": {
"en": "Sample description",
},
"category_ids": [
{
"id": 2
},
{
"id": 5
}
],
}
所以我想要做的是确保给定一个ID为5的类别,我希望前两个项目是前两个出现。
这可以在流星中完成,而不必诉诸在JS中编写额外的逻辑?我曾经做过的一种方法是从客户端集合中更新每个项目(我不再做),并设置一些额外的属性,然后再进行排序。
在处理同步客户端和服务器集合时,这并不可行。
感谢您的回答,但我不是试图过滤文件,只显示集合中的某些文件。我想显示所有文件,但应该先出现的文件是类别编号为5的文件。 – matfin 2014-09-19 06:44:52
我的不好!我刚刚更新了我的答案,以包含所有文档。 – Neil 2014-09-19 14:34:37
这是一个现在可以工作的解决方案,它足够干净。谢谢。 – matfin 2014-09-19 18:51:31