2015-11-19 21 views
0

在我的網頁,裏面有URL object/:_id,用戶通常應該訪問只是一個文件,我已經設置了像這樣:如何到達Iron Router內的數據?

waitOn: function() { 

    return Meteor.subscribe('objects', this.params._id), 
}, 

data: function() { 
    return Objects.findOne({_id: this.params._id}) 
} 

然而,也應儘可能採取在偷看和與其他一些物體,但只有那些相同的顏色,因爲我們正在尋找對象的工作,所以我需要有這些訪問爲好。

這裏就是我想會的工作:

onBeforeAction: function() { 
    var self = Objects.findOne({_id: this.params._id}) 
    var color = self.color 
    Session.set('chosenColor', color) 
    this.next() 
}, 

waitOn: function() { 
    return Meteor.subscribe('objects', Session.get('chosenColor')) 
}, 

data: function() { 
    return Objects.findOne({_id: this.params._id}) 
} 

應當注意的是,這個工作在第一,但隨後突然莫名其妙地停止工作。由於某種原因,現在總是「未定義」self

什麼是鐵路由器中訪問這個數據的正確方法是什麼?

回答

1

你有循環邏輯在這裏:你想裝載對象和之前得到它的顏色您訂閱那將集合發佈該對象。只需將顏色邏輯移動到服務器上的發佈功能即可。

客戶端JS:

waitOn: function() { 

    return Meteor.subscribe('objects', this.params._id); 
}, 

data: function() { 
    return Objects.findOne({_id: this.params._id}) 
} 

服務器JS:

Meteor.publish('objects',function(id){ 
    check(id, Meteor.Collection.ObjectID); 
    var self = Objects.findOne({_id: id}) 
    if (self){ 
    var color = self.color; 
    return Objects.find({ color: color }); 
    } 
    else this.ready(); 
}); 
0

this.ready()條件的數據嘗試:

waitOn: function() { 
    return Meteor.subscribe('objects'); 
}, 

data: function() { 
    if (this.ready()) { 
     var self = Objects.findOne({_id: this.params._id}); 
     var color = self.color; 

     return Objects.findOne({_id: this.params._id}, color); 
    } 
} 
+0

不工作,我不希望數據上下文是所有這些對象的在顏色。 – Yeats

相關問題