2015-04-03 91 views
1

請參閱下面的代碼,請:訪問鐵路由器數據

Router.route('/posts/:_id', function() { 
    this.render('Post', { 
    to: 'content', 
    data: function() { 
     return Posts.findOne({id: this.params._id}); 
    } 
    }); 
}); 

如果Post對象有titlebody的Fileds在MongoDB中,我可以從Post.html模板訪問他們喜歡

<h4>Post title: {{title}}</h4> 
<h3>Post body: {{body}}</h4> 

我想在模板幫助函數中訪問Post對象Post.js。可能嗎?

更新: 根據這個問題:Meteor data-context with iron-router,我可以訪問data變量是這樣的:

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

有沒有辦法做到這裏面Template.Post.events

回答

2

好像你正在尋找Template.currentData()方法。

Template.example.events({ 
    'click #test':function(e,t){ 
     console.log(Template.currentData()) 
    } 
}) 

更新好像使用CURRENTDATA有根據check this

如此看來,如果你想使用它等的情況下型動物的行爲,你應該是一個DOM元素中。

Template.post.events({ 
    'click h4':function(){ 
    console.log(Template.currentData()) // and should return the title. 
    } 
}) 

根據stubalio說。

在事件處理程序中,返回 激發事件的元素的數據上下文。