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?

TheNastyOne
  • 965
  • 11
  • 19
  • Well you are removing spaces so of course it can't find it. Consider adding a [slug](https://en.wikipedia.org/wiki/Semantic_URL#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})`. – user3557327 Nov 10 '14 at 23:01
  • 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. – TheNastyOne Nov 10 '14 at 23:42

1 Answers1

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
        };
    }
);
solarc
  • 5,638
  • 2
  • 40
  • 51
  • 2
    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. – TheNastyOne Nov 15 '14 at 01:53
  • and how can I pass this into my angular controller after rendering the new page? – Marian Klühspies Sep 04 '15 at 15:28
  • @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. – solarc Sep 09 '15 at 04:28
  • 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" – Savvas Parastatidis Sep 29 '16 at 11:34
  • @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})};` – solarc Sep 29 '16 at 19:58