Do RouteControllers need to be defined in server code for iron-router@1.0.0-pre3

ε祈祈猫儿з 提交于 2019-12-11 09:12:36

问题


While attempting to migrate from Meteor 0.8.3 & IR 0.8.2 to Meteor 0.9.3.1 & IR 1.0.0-pre3 I'm running into a problem with RouteControllers.

I have the following js in the '/both' folder of the project:

Router.route('scene.index', {
    path: '/',
    controller: 'SceneController'
});

And the SceneController js in the '/client' tree:

SceneController = RouteController.extend({
    template: 'SceneView'
    ...
});

When attempting to access the route from the client, I get the following error:

Error: RouteController 'SceneController' is not defined.
    at resolve (packages/iron:router/lib/route.js:94)
    at Function.Route.findControllerConstructor (packages/iron:router/lib/route.js:116)
    at Function.Route.createController (packages/iron:router/lib/route.js:134)
    at Function.Router.createController (packages/iron:router/lib/router.js:181)
    at Function.Router.dispatch (packages/iron:router/lib/router_server.js:66)
    at Object.router (packages/iron:router/lib/router.js:15)
    at next (/Users/pward/.meteor/packages/webapp/.1.1.2.1m8ln9s++os+web.browser+web.cordova/npm/node_modules/connect/lib/proto.js:190:15)
    at Function.app.handle (/Users/pward/.meteor/packages/webapp/.1.1.2.1m8ln9s++os+web.browser+web.cordova/npm/node_modules/connect/lib/proto.js:198:3)
    at Object.fn [as handle] (/Users/pward/.meteor/packages/webapp/.1.1.2.1m8ln9s++os+web.browser+web.cordova/npm/node_modules/connect/lib/proto.js:74:14)
    at next (/Users/pward/.meteor/packages/webapp/.1.1.2.1m8ln9s++os+web.browser+web.cordova/npm/node_modules/connect/lib/proto.js:190:15)

After a little quality time with node-inspector, I've come to the conclusion that IR needs client-side RouteControllers to be visible on the server.

My route controllers currently set session variables and I'd prefer not to sprinkle Meteor.isClient around. Is this an IR bug or a known breaking change?


回答1:


My route controllers currently set session variables and I'd prefer not to sprinkle Meteor.isClient around. Is this an IR bug or a known breaking change?

You don't need to guard your controller code with Meteor.isClient because it's going to be executed only in the client side even if it's declared in the shared folder.

Your route definition is better written like this in iron:router@1.0.0-pre3 :

Router.route('/', {
  name: 'scene.index',
  controller: 'SceneController'
});

name/path have switched their position.

EDIT :

As far as iron:router concepts are concerned, please read this :

http://eventedmind.github.io/iron-router/#client-and-server

Having routes defined on both the client and the server is mandatory to determine what needs to be done.

Client-side, if a link to a client route is detected then we can navigate to it using the HTML5 pushState API, if a server route is detected then standard HTTP communication takes place (for example I use server routes to provide downloadable resources in my app), if the link corresponds to no known path to the router, then an error is triggered.

Server side, it's important to know about client routes to send 404 on unknown paths instead of serving the Meteor app, here is a quote about this from the official guide :

It also means that on the server, if there is no client route defined, we can send a 404 response to the client instead of loading up the Meteor application.




回答2:


Yes, I've always placed my router.js in the shared common code, which is any folder not named server/ or client/.



来源:https://stackoverflow.com/questions/26343016/do-routecontrollers-need-to-be-defined-in-server-code-for-iron-router1-0-0-pre3

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