How do I activate an Iron Router route without changing the path?

倖福魔咒の 提交于 2019-12-13 12:41:44

问题


In my Meteor app, I'm trying to pass data from my Iron Router controller to a template that's a partial in my app. How do I activate a route without changing the path? I don't want to switch pages, only render the partial with my data (if that makes sense).

Here's how the app works:

Right now, the route is activated when I click a dropdown link in my navbar

Template.navbar.events
  'click #threads-link': (event)->
    Router.go 'allThreads'

This renders the template with my data as long as I have a path in my route. But since I don't want the path to change, I tried leaving the path--and then the template doesn't retrieve the data from my controller!

Router.map ->
  @route "allThreads",
    controller: ThreadsController 

Do you know how to get my template partial to access the data in the controller without changing the path? Thanks in advance!


回答1:


First, you're probably seeing some issues because the latest version of Iron Router requires you to be on Meteor v0.8.0. If you're on a previous version of Meteor you'll need to use the older Iron Router (0.6).

To customize rendering inside of your ThreadsController you can just override the action function, or you can define a hook. Then, you can call the render method to render whichever templates you'd like. Here is an example:

ThreadsController = RouteController.extend({
  action: function () {
    // Example 1. render this controller's default template and all yields
    this.render();

    // Example 2. render myTemplate into the main yield
    this.render('myTemplate');

    // Example 3. render templates into yield regions
    this.render('myFooter', {to: 'footer'});
  }
}); 


来源:https://stackoverflow.com/questions/22772029/how-do-i-activate-an-iron-router-route-without-changing-the-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!