Grab attribute of current record for Meteor Method Call

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 22:02:42

问题


I am working on a project to pull in twitter timeline's for selected teams/players. When I am on the team/_id page, how can I grab an attribute to path through a method?

Below is my client side javascript, as well as the relevant route from iron router. When I type in something like "patriots" to the "????" section, I get a result. I would like to do this dynamically, I currently have the twitter handles stored under the twitter attribute.

Template.tweets.helpers({
	teams: function() {
    return Teams.find();
  },
});

Template.tweets.onRendered(function () {
	var twitterHandle = "???";
    Meteor.call('getTimeline', twitterHandle, function(err,results){
    	if (err) {
    		console.log("error", error);
    	};
    	console.log(results);
    	Session.set("twitter", JSON.parse(results.content));
    })
    return Session.get("twitter");
});


Router.route('/teams/:_id', {
    name: 'teamView',
    template: 'teamView',
    data: function(){
        var currentTeam = this.params._id;
        return Teams.findOne({ _id: currentTeam });
        var twitterHandle = this.params.twitter;
        return Teams.findOne({twitter: twitterHandle});
    }
});
<template name="tweets">
	<h3>Tweets</h3>
	<div class="container">
		{{twitter}}
	</div>

</template>

回答1:


You should be able to access all information from the current route using the Router.current() object. In your case you can use Router.current().params._id to get the _id param:

var twitterHandle = Router.current().params._id;

Edits based on your comments below

I did not notice that you were calling the Teams.findOne function twice in your route's data function. Form the looks of it you're already storing the twitter handle in the Teams collection, so you merely need to access the data that's returned by the route.

Template.tweets.helpers({
    twitterData: function() {
        //return the data stored in the callback function of the Meteor method call in the onRendered event
        return Session.get('twitter');
    }
});

Template.tweets.onRendered(function () {
    //clear any previously stored data making the call
    Session.set('twitter', null);

    //property of the team document returned by the data function in the route.
    var twitterHandle = this.data.twitter;

    Meteor.call('getTimeline', twitterHandle, function(err,results){
        if (err) {
            console.log("error", error);
        } else {
            Session.set("twitter", JSON.parse(results.content));
        }
    });
});


Router.route('/teams/:_id', {
    name: 'teamView',
    template: 'teamView',
    data: function(){
        var currentTeam = this.params._id;
        return Teams.findOne({ _id: currentTeam });
    }
});

<template name="tweets">
    <h3>Tweets</h3>
    <div class="container">
        <!-- twitterData will be an object, so you'll need to figure out what properties to display and use dot notation//-->
        {{twitterData}}
    </div>
</template>


来源:https://stackoverflow.com/questions/31931330/grab-attribute-of-current-record-for-meteor-method-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!