2015-02-10 29 views
0

這是代碼:即使我使用waitOn,集合也沒有及時呈現?

Meteor.publish('singleDocument', function(documentId) { 
    return Documents.find(documentId); 
}); 

this.route("documentPage", { 
    path: "/documents/:_id", 
    waitOn: function() { 
    return Meteor.subscribe("singleDocument", this.params._id); 
    }, 
    data: function() { 
    return Documents.findOne(this.params._id); 
    } 
}); 

Template.documentPage.rendered = function() { 
    Tracker.autorun(function() { 
     console.log(this.content); 
    }); 
}; 

正如你可以看到我已經設置的一切,我等待的收集與waitOn。但console.log(this.content);仍然會返回undefined,好像該集合尚未加載。

可能是什麼問題?

回答

1

this自動運行中沒有引用用於呈現模板的數據上下文。在渲染功能,自動運行功能之外(你不應該需要使用自動運行的話),你可以使用this.data指用來渲染模板的數據上下文,所以請嘗試以下操作:

Template.documentPage.rendered = function() { 
    console.log(this.data); 
}; 

根據下面的評論更新:

你應該能夠使用Template.currentData監聽數據環境的變化:

Template.documentPage.rendered = function() { 
    Tracker.autorun(function(){ 
     console.log(Template.currentData()) 
    }) 
}; 
+0

你說得對,它的工作。謝謝。那麼在Tracker.autorun中沒有辦法使用它?如果我想在每次收集更新時要運行的函數中使用它, – alexchenco 2015-02-11 03:34:29

+0

@alexchenco,我認爲你可以使用Template.currentData。看到我更新的答案。 – 2015-02-11 07:28:50

相關問題