2016-05-23 55 views
0

所以在圍繞堆棧溢出尋找解決此問題的方法之後,我遇到了這個https://dweldon.silvrback.com/common-mistakes和這個https://dweldon.silvrback.com/guards兩者都非常有用。流星模板加載兩次一次集合undefined一次定義它

我的問題是,我的模板之一加載兩次,一次與集合undefined,然後再次與定義的集合。所以在控制檯中我看到這樣的:

TRADEID 
undefined 
TRADEID 
L…n.Cursor {collection: LocalCollection, sorter: null, matcher: M…o.Matcher, _selectorId: undefined, skip: undefined…} 

在我試圖解決這個問題,我已經實現了防守,所以我的JS文件看起來像這樣

TRADEINFO = new Mongo.Collection("trade_info"); 

Template.E4E_tradeTile.onCreated(function(){ 
    this.subscribe('users'); 
    this.subscribe('trade_info'); 
}); 

Template.E4E_tradeTile.helpers({ 
    borrow(){ 
    console.log(this.tradeID); 
    var guard = TRADEINFO.findOne(); 
    var query = TRADEINFO.find(); 
    console.log(guard && query); 
    return guard && query; 
    } 
}); 

,然後在我的模板我讀出這借用

<h2 class="no-margins">{{borrow.element}}</h2> 
      <small>Borrow</small> 

模板似乎總是呈現空白,(沒有文字),所以我想,當模板被renderd集合爲暫無數據,那麼集合時變得可用,它不更新。

再次感謝您的幫助!

回答

0

因此,事實證明這兩種方法的結合似乎解決了這個問題。我加入了這條路線:

Router.route('/problem', { 

    waitOn: function() { 
    // return one handle, a function, or an array 
    return Meteor.subscribe('trade_info', this.params._id); 
    }, 

action: function() { 
    if(Meteor.userId()){ 
    this.render('problem_page'); 
    }else { 
    this.render('login'); 
    this.layout('blankLayout'); 
    } 
} 

並使用了警衛!如上所示