Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 views
in Technique[技术] by (71.8m points)

meteor - meteorjs iron-router waitOn and using as data on rendered

I try to get the returned data in my Template.rendered function.

The current code is:

this.route('editCat', {
    layoutTemplate : 'layoutCol2Left',
    template : 'modCategoriesEdit',
    path : '/mod/categories/edit/:_id',
    yieldTemplates : _.extend(defaultYieldTemplates, {
    'navigationBackend' : {to : 'contentLeft'} 
    }),
    waitOn : function () {
         return Meteor.subscribe('oneCat', this.params._id);
    },
    data : function () {
         return Categories.findOne({_id : this.params._id});
    }
 });

In this block i wait on the subscribtion of the Collection Document and return the Document as data.

Now i can use the returned Document in my Template like this:

<template name="modCategoriesEdit"> 
    <h1>Edit {{name}}</h1>
</template>

My problem is that i have to use the returned data in my rendered function like this:

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

But this returns "null".

So my question is: How is it possible to get access to the returned data in the rendered function ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Solution:

Just add the following to your iron-router route() method.

action : function () {
    if (this.ready()) {
        this.render();
    }
}

Than the Template will rendered after all is loaded correctly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...