流星,铁:路由器在Router.go上传递多个属性 [英] Meteor, Iron:Router Passing Multiple Properties on Router.go

查看:91
本文介绍了流星,铁:路由器在Router.go上传递多个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的困境是我想将多个对象属性传递给Meteor中的iron:router路由。原因是我想向其传递一个属性来命名我的url和一个属性来查找一个集合项。它们彼此完全独立,我不能使用url属性,因为它不是收集项中的值。这就是我所拥有的:

My dilemma is that I would like to pass multiple object properties to an iron:router route in Meteor. The reasoning is that I would like to pass it a property to name my url with and a property to find a collection item with. They are completely independent of each other and I can't use the url property because it is not a value in the collection item. This is what I have:

Template.items.events({
'click': function () {
    itemName = this.name.replace(/ /g,'')
    Router.go('itemDetails', {itemName: itemName})
    }
});

问题是,尽管路由器可以处理此问题并将其发送到正确的网址,但我无法使用 itemName 来查找我正在寻找的收集项对象(假设这是不可能的)。

The problem is that although the Router handles this fine and sends me to the correct url, I cannot use itemName to find the collection item object that I am looking for (assume this is impossible).

Router.route('/items/:itemName', {
    name: 'itemDetails', 
    data: function() {return Items.findOne({name: this.params.itemName})}
});

上述路由器配置不会返回任何内容,因为 name!= this.params .itemName 用于任何对象。

The above Router configuration will not return anything because name != this.params.itemName for any object.

我尝试传递 this 对象,或创建具有多个属性的对象,但iron:router不会没有它。

I've tried passing the this object, or creating objects with multiple properties, but iron:router won't have it.

感谢您的帮助。

编辑#1:为了进一步说明问题,我的问题与路由到使用URL中多个ID的页面相同。例如,如何将属性传递给iron:router以填充:_ id :itemId 属性?

Edit #1: To help explain the question further, my problem is the same as routing to a page that uses multiple id's in the URL. For example, how would I go about passing properties to iron:router to fill the :_id and :itemId properties?

Router.route('items/:_id/:_itemId', {
    name: 'detailDetails',
    data: function() {...}
});

编辑#2:我要具体做的是将两个属性传递给iron:router并具有其中一个附加到URL,另一个用于路由的data属性以返回收集项。例如:

Edit #2: What I would like to do specifically is pass two properties to iron:router and have one of them be appended to the URL, and the other be used in the data property of the route to return a collection item. Example:

....
    Router.go('itemDetails', {_id: itemBeingPassedId, itemName: nameToBeAppendedToURL})
....

Router.route('/items/:itemName', {
    name: 'itemDetails',
    data: function(){return Items.findOne(_id)
});

每当我尝试这样做时,它都会说 _id 未定义。因此,基本上,如何将属性传递给 data 而不将其作为URL的一部分并使用 this.params

Whenever I try to do that, it says that _id is undefined. So basically, how can I pass a property to data without having it be a part of the URL and using this.params?

推荐答案

问题是如何将多个参数传递给Router.go?只需将它们全部放入第二个参数的对象中即可:

Is the question how to pass multiple parameters to Router.go? Just put all of them in the object for the second parameter:

Router.go('itemDetails', {_id: 'foo', '_itemId': bar});

编辑:

好吧,如果您要将任意值传递给url,可以使用查询参数:

Ok, if you want to pass arbitrary values to the url, you can use query paramters:

Router.go('itemDetails', {itemName: 'foo'}, {query: 'id=bar'});

该ID仍将保留在URL中,如下所示:

The id will still be in the url though, it will look like this:

http://example.com/items/foo?id=bar

您可以像这样检索它:

Router.route('/items/:itemName', {
    name: 'itemDetails',
    data: function(){
        return {
            item: Items.findOne(this.params.query.id),
            itemName: this.params.itemName
        };
    }
);

这篇关于流星,铁:路由器在Router.go上传递多个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆