【问题标题】:Grab attribute of current record for Meteor Method Call获取 Meteor 方法调用的当前记录的属性
【发布时间】:2015-08-12 03:09:40
【问题描述】:

我正在开展一个项目,为选定的团队/玩家提供推特时间线。当我在 team/_id 页面时,如何通过方法获取路径的属性?

以下是我的客户端 javascript,以及来自 Iron 路由器的相关路由。当我在“????”中输入“爱国者”之类的内容时部分,我得到一个结果。我想动态执行此操作,我目前将 twitter 句柄存储在 twitter 属性下。

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>

【问题讨论】:

    标签: javascript meteor


    【解决方案1】:

    您应该能够使用Router.current() 对象从当前路由访问所有信息。在您的情况下,您可以使用 Router.current().params._id 获取 _id 参数:

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

    根据您的以下 cmets 进行的修改

    我没有注意到您在路由的 data 函数中调用了两次 Teams.findOne 函数。从外观上看,您已经将 twitter 句柄存储在 Teams 集合中,因此您只需要访问路由返回的 data

    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>
    

    【讨论】:

    • 好的,谢谢。如果我想获取当前 _id 的“twitter”属性会是什么? var twitterHandle = Router.current().params.twitter; ?之前尝试过没有任何运气
    • 对不起。我没有在代码打击中看到这一点。我没有看到路由中定义的twitter 参数?您是否将其作为查询字符串传递?如果您希望它作为参数可用,则需要将其定义为路由字符串/teams/:_id/:twitter 的一部分。类似的东西。
    • 如果使用查询字符串传入推特句柄,可以使用Router.current().params.query.twitter获取值。
    • 没有尝试创建新路线 (/teams/:_id/:twitter),但 Router.current().params.query.twitter 没有运气:/ 我是新手,与这家伙摔跤至少 12 个小时
    • 添加了一些建议。希望有帮助
    猜你喜欢
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 2023-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多