Meteor, Iron:Router Passing Multiple Properties on Router.go
Asked Answered
E

1

9

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})
    }
});

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})}
});

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

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

Any help is appreciated, thanks.

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() {...}
});

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)
});

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?

Embargo answered 10/11, 2014 at 19:52 Comment(2)
Well you are removing spaces so of course it can't find it. Consider adding a slug property to your collection objects (on creation or when the itemName changes) that way you just use Router.go('itemDetails', {slug: this.slug}) and Items.findOne({slug: this.params.slug}).Asp
Yea I know the reasoning as to why it can't be found- I think I did a poor job explaining that. I was trying to avoid using a slug but it seems like that might be the only way around it. Thanks for the help.Embargo
H
8

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});

Edit:

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

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

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

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

And you can retrieve it like this:

Router.route('/items/:itemName', {
    name: 'itemDetails',
    data: function(){
        return {
            item: Items.findOne(this.params.query.id),
            itemName: this.params.itemName
        };
    }
);
Hypostasis answered 11/11, 2014 at 2:52 Comment(5)
No, not exactly. I'm wondering why I can't pass in multiple properties to iron:router, and call one of those properties ONLY in the data property. I'm about to update my question to make it more specific.Embargo
and how can I pass this into my angular controller after rendering the new page?Cramfull
@Nachbar90, I haven't worked with angular but if it's anything like blaze then whatever you return in data will be available to your templates.Hypostasis
Is there any way to pass parameters or data in general, without them showing in the url? I would prefer that the user sees "example.com/overview/UPS" rather than "example.com/overview/UPS?id=NkYfJsJyATcHDM2om"Pronuba
@Savvas, you can pull stuff from the database based on whatever you have in the url, just query the db for something other than the _id: return {item: Items.findOne({"itemName":this.params.itemName})};Hypostasis

© 2022 - 2024 — McMap. All rights reserved.