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