Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
486 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...