更新:已解決此問題,並修改代碼以反映我添加的更改。我也強烈推薦閱讀這篇文章下面的評論。我們需要注意IronRouter的一些變化。流星ironrouter - 即使我打電話waitOn()收集返回undefined()
關鍵的東西是以下內容添加到您的數據功能:
if(!this.ready()) {
return;
}
我有我的應用程序與IronRouter的問題。代碼如下:
/**
* 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.ready()) {
return;
}
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
*/
var category = App.models.categories.findOne(query);
console.log(category);
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,這是我需要另一個查詢。
問題是,當我重新加載頁面和上面給出的路徑匹配時,這條路線似乎運行了三次,並且第一次在控制檯上記錄類別變量返回未定義,然後又兩次,實際的類別被打印出來。
我希望waitOn()函數在我到達數據函數的時候已經填充了所有類別,因此我可以在第一次使用查詢時訪問我的類別數據,但這不會發生。
任何人都可以看到在我的代碼中可能會導致此問題明顯顯而易見的任何事情,爲什麼路線正在運行三次?
我使用最新版本的IronRouter(iron:[email protected])和Meteor([email protected])。
謝謝你的回答。這已經解決了這個問題。 – matfin 2014-09-19 18:59:33
增加了一些關於'iron:router'最新版本的評論。 – saimeunt 2014-09-19 19:05:20
嗨,github的鏈接目前是404ing :(我很期待閱讀哈哈:) – lol 2015-03-10 02:24:43