0

I'm having some trouble trying to use the URL parameters in meteor with iron:router.

On the home page if my app, I display a thread of posts. But I want to allow people to select a single thread by specifying its ID in the url. Like that, when someone click on a link to a specific thread like myapp/the_asked_id, he only see the data related to the thread. Otherwise, he see all threads.

I think it would be something where I get the data from the url like in PHP, and then check for the value / existence of this parameter : if it exists / has a correct value, I display the thread, otherwise I display all of them.

Here is what I've tried so far :

HTML link :

<a href="#" class="dropdown-toggle green-link" data-toggle="dropdown" title="Notifications" data-target="{{link}}">the link</a>

with {{link}} to be the _id from my collection to identify the required thread / doc to display.

Js link event :

'click .talkLink' : function(event, template){
      if($(event.target).hasClass('talkLink'))
      {
        console.log(event.target.dataset.target);
        console.log(event.currentTarget);
      }
      else {
        console.log(event.currentTarget);
      }
      Router.go('/', {_id: event.target.dataset.target});
    },

Router :

this.route('home', {
      path: '/:_id?',
      data: {
        threadId: (this.params._id || "")
      }
    });

But I can't get it working, as it currently always display me the not found template...

Could someone explain me what is going wrong and how I should proceed ?

Thank you,

David

2
  • At first glance, I don't see that you have a template called home. Where are the <template name="home"></template> tags? Commented Jun 17, 2015 at 12:44
  • Also, data points to a function that returns something, not an object. github.com/iron-meteor/iron-router/blob/devel/… Commented Jun 17, 2015 at 12:47

1 Answer 1

3

A few suggestions:

this.route('home', {
    path: '/:_id',
    data: function() {
        // assuming you have a collection called "Threads"
        return Threads.findOne(this.params._id);
    }
});
  1. You need a template called 'home'. It's data context will be the return of the data callback
  2. You do not need to place a ? in the path
  3. data is a callback, in which you can access this.params within to find the correct document.
Sign up to request clarification or add additional context in comments.

2 Comments

I have a template Home, i just didn't copied it here :P Doesn't the ? means that this parameters is optionnal ?
Honestly I didn't know about the ?. Either way, you could rethink the route name, instead of placing the _id on the home route, instead place it on a /threads/:_id path

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.