├── .gitignore ├── .npm └── package │ ├── .gitignore │ ├── README │ └── npm-shrinkwrap.json ├── .travis.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── client ├── _init.js ├── group.js ├── modules.js ├── route.js ├── router.js └── triggers.js ├── lib └── router.js ├── package.js ├── server ├── _init.js ├── group.js ├── plugins │ └── fast_render.js ├── route.js └── router.js └── test ├── client ├── _helpers.js ├── group.spec.js ├── loader.spec.js ├── route.reactivity.spec.js ├── router.core.spec.js ├── router.reactivity.spec.js ├── router.subs_ready.spec.js ├── trigger.spec.js └── triggers.js ├── common ├── fast_render_route.js ├── group.spec.js ├── route.spec.js ├── router.addons.spec.js ├── router.path.spec.js └── router.url.spec.js └── server ├── _helpers.js └── plugins └── fast_render.js /.gitignore: -------------------------------------------------------------------------------- 1 | .build* 2 | *.browserify.js.cached 3 | *.browserify.js.map 4 | .idea -------------------------------------------------------------------------------- /.npm/package/.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.npm/package/README: -------------------------------------------------------------------------------- 1 | This directory and the files immediately inside it are automatically generated 2 | when you change this package's NPM dependencies. Commit the files in this 3 | directory (npm-shrinkwrap.json, .gitignore, and this README) to source control 4 | so that others run the same versions of sub-dependencies. 5 | 6 | You should NOT check in the node_modules directory that Meteor automatically 7 | creates; if you are using git, the .gitignore file tells git to ignore it. 8 | -------------------------------------------------------------------------------- /.npm/package/npm-shrinkwrap.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "page": { 4 | "version": "1.6.4", 5 | "resolved": "https://github.com/kadirahq/page.js/archive/34ddf45ea8e4c37269ce3df456b44fc0efc595c6.tar.gz", 6 | "from": "https://github.com/kadirahq/page.js/archive/34ddf45ea8e4c37269ce3df456b44fc0efc595c6.tar.gz", 7 | "dependencies": { 8 | "path-to-regexp": { 9 | "version": "1.2.1", 10 | "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-1.2.1.tgz", 11 | "from": "path-to-regexp@>=1.2.1 <1.3.0", 12 | "dependencies": { 13 | "isarray": { 14 | "version": "0.0.1", 15 | "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", 16 | "from": "isarray@0.0.1" 17 | } 18 | } 19 | } 20 | } 21 | }, 22 | "qs": { 23 | "version": "5.2.0", 24 | "resolved": "https://registry.npmjs.org/qs/-/qs-5.2.0.tgz", 25 | "from": "qs@5.2.0" 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | language: node_js 3 | node_js: 4 | - "0.10" 5 | before_install: 6 | - "curl -L http://git.io/ejPSng | /bin/sh" 7 | env: 8 | - TEST_COMMAND=meteor -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ### v2.12.1 4 | 5 | * Add NPM modules back. Fixes: [#602](https://github.com/kadirahq/flow-router/issues/602) 6 | 7 | ### v2.12.0 8 | 9 | * Update Fast Render to v2.14.0 10 | 11 | ### v2.11.0 12 | 13 | * Add support for Meteor 1.3 RC-1. 14 | * Removes browserify and get modules from Meteor 1.3. 15 | 16 | ### v2.10.1 17 | * Fix the url generation for prefixed paths. See: [#508](https://github.com/kadirahq/flow-router/issues/508) 18 | 19 | ### v2.10.0 20 | * Update few dependencies to the latest versions: pagejs, qs, cosmos:browserify 21 | 22 | ### v2.9.0 23 | * Add FlowRouter.url() See: [#374](https://github.com/kadirahq/flow-router/pull/374) 24 | 25 | ### v2.8.0 26 | * Allow to access options in groups as well. See: [#378](https://github.com/kadirahq/flow-router/pull/378) 27 | 28 | ### v2.7.0 29 | * Add Path Prefix support. See: [#329](https://github.com/kadirahq/flow-router/pull/329) 30 | 31 | ### v2.6.2 32 | * Now .current() sends a cloned version of the internal current object. Which prevent outside mutations to params and queryParams 33 | 34 | ### v2.6.1 35 | 36 | * Fix [#143](https://github.com/kadirahq/flow-router/issues/314). 37 | This says that when we are doing a trigger redirect, 38 | We won't get reactive changes like: `getRouteName()` 39 | 40 | ### v2.6.0 41 | * Add hashbang support. See [#311](https://github.com/kadirahq/flow-router/pull/311) 42 | 43 | ### v2.5.0 44 | * Add a stop callback on the triggers. See: [#306](https://github.com/kadirahq/flow-router/pull/306). 45 | 46 | ### v2.4.0 47 | 48 | * Add a name to the route groups. See: [#290](https://github.com/kadirahq/flow-router/pull/290) 49 | 50 | ### v2.3.0 51 | * We've used `path` for both the current path and for the pathDef earlier. Now we differentiate it. See: [#272](https://github.com/kadirahq/flow-router/issues/272) and [#273](https://github.com/kadirahq/flow-router/pull/273) for more information. 52 | 53 | ### v2.2.0 54 | * Add the first addOn api: FlowRouter.onRouteRegister(cb) 55 | 56 | ### v2.1.1 57 | * There was an issue in IE9 support. We fix it with this version. 58 | 59 | ### v2.1.0 60 | * Add IE9 Support. See this issue [#111](https://github.com/kadirahq/flow-router/issues/111) for more info. 61 | 62 | ### v2.0.2 63 | 64 | * Add missing queryParams object in the subscriptions method (with FR on the server) 65 | * With that, [#237](https://github.com/kadirahq/flow-router/issues/237) is partially fixed. 66 | 67 | ### v2.0.1 68 | 69 | * Use pagejs.redirect() for our redirection process. 70 | * Above fixes [#239](https://github.com/kadirahq/flow-router/issues/239) 71 | 72 | ### v2.0.0 73 | 74 | * Released 2.0 :) 75 | * Now flow-router comes as `kadira:flow-router` 76 | * Remove deprecated APIs 77 | - `FlowRouter.reactiveCurrent()` 78 | - Middlewares 79 | - `FlowRouter.current().params.query` 80 | * Follow the [migration guide](https://github.com/kadirahq/flow-router#migrating-into-20) for more information. 81 | 82 | ### v1.18.0 83 | 84 | * Implement idempotent routing on withReplaceState. See: [#197](https://github.com/meteorhacks/flow-router/issues/197) 85 | * Add an [API](https://github.com/meteorhacks/flow-router#flowrouterwithtrailingslashfn) to set trailing slashes. 86 | 87 | ### v1.17.2 88 | * Fix [#182](https://github.com/meteorhacks/flow-router/issues/182) - Now trigger's redirect function support `FlowRouter.go()` syntax. 89 | 90 | ### v1.17.1 91 | 92 | * Fix [#164](https://github.com/meteorhacks/flow-router/issues/164) - It's an issue when using `check` with flow router query params. 93 | * Fix [#168](https://github.com/meteorhacks/flow-router/pull/168) - It's URL encoding issue. 94 | 95 | ### v1.17.0 96 | 97 | * Add an API called `FlowRouter.wait()` to wait the initialization and pass it back to the app. Fixes issue [180](https://github.com/meteorhacks/flow-router/issues/180). 98 | 99 | ### v1.16.3 100 | 101 | * Fix a crazy context switching issue. For more information see commit [6ca54cc](https://github.com/meteorhacks/flow-router/commit/6ca54cc7969b3a8aa71d63c98c99a20b175125a2) 102 | 103 | ### v1.16.2 104 | * Fix issue [#167](https://github.com/meteorhacks/flow-router/issues/167) via [#175](https://github.com/meteorhacks/flow-router/pull/175) 105 | * Fix [#176](https://github.com/meteorhacks/flow-router/issues/176) by the removal of `Tracker.flush` usage. 106 | 107 | ### v1.16.1 108 | * Fix [issue](https://github.com/meteorhacks/flow-router/pull/173) of overwriting global triggers when written multiple times. 109 | 110 | ### v1.16.0 111 | 112 | * [Refactor](https://github.com/meteorhacks/flow-router/pull/172) triggers API for clean code 113 | * Added [redirect](https://github.com/meteorhacks/flow-router#redirecting-with-triggers) functionality for triggers 114 | * Now we are API complete for the 2.x release 115 | 116 | ### v1.15.0 117 | 118 | * Now all our routes are idempotent. 119 | * If some one needs to re-run the route, he needs to use our `FlowRouter.reload()` API. 120 | 121 | ### v1.14.1 122 | 123 | * Fix regression came from v1.11.0. With that, `FlowRouter.go("/")` does not work. More information on [#147](https://github.com/meteorhacks/flow-router/issues/147). 124 | 125 | ### v1.14.0 126 | * Bring browserify back with the updated version of `cosmos:browserify` which fixes some size issues. See [more info](https://github.com/meteorhacks/flow-router/issues/128#issuecomment-109799953). 127 | 128 | ### v1.13.0 129 | * Remove browserified pagejs and qs dependency loading. With that we could reduce ~10kb of data size (without compression). We can look for a bower integration in the future. For now, here are the dependencies we have. 130 | - page@1.6.3: https://github.com/visionmedia/page.js 131 | - qs@3.1.0: https://github.com/hapijs/qs 132 | 133 | ### v1.12.0 134 | * Add [`FlowRouter.withReplaceState`](https://github.com/meteorhacks/flow-router#flowrouterwithreplcaestatefn) api to use replaceState when changing routes via FlowRouter apis. 135 | 136 | ### v1.11.0 137 | * Fix [#145](https://github.com/meteorhacks/flow-router/issues/145) by changing how safeToRun works. 138 | * Add `FlowRouter.path()` to the server side 139 | * Fix [#130](https://github.com/meteorhacks/flow-router/issues/130) 140 | 141 | ### v1.10.0 142 | Add support for [triggers](https://github.com/meteorhacks/flow-router#triggers). This is something similar to middlewares but not as middlewares. Visit [here](https://github.com/meteorhacks/flow-router/pull/59) to learn about design decisions. 143 | 144 | _**Now, middlewares are deprecated.**_ 145 | 146 | ### v1.9.0 147 | Fix [#120](https://github.com/meteorhacks/flow-router/issues/120) and added callback support for `FlowRouter.subsReady()`. 148 | 149 | ### v1.8.0 150 | 151 | This release comes with improvements to the reactive API. 152 | 153 | * Fixed [#77](https://github.com/meteorhacks/flow-router/issues/77), [#85](https://github.com/meteorhacks/flow-router/issues/85), [#95](https://github.com/meteorhacks/flow-router/issues/95), [#96](https://github.com/meteorhacks/flow-router/issues/96), [#103](https://github.com/meteorhacks/flow-router/issues/103) 154 | * Add a new API called `FlowRouter.watchPathChange()` 155 | * Deprecated `FlowRouter.reactiveCurrent()` in the favour of `FlowRouter.watchPathChange()` 156 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Whether to submit an issue or not? 2 | 3 | We've very limited time to answer all the issues and respond them in a proper manner. 4 | So, this repo's issue list only used to report **bugs** and **new features.** 5 | 6 | For any other questions, issues or asking for best practices use [Meteor Forums](https://forums.meteor.com/). 7 | Even before you ask a question on Meteor Forums, make sure you read the [Meteor Routing Guide](https://kadira.io/academy/meteor-routing-guide). 8 | 9 | ## Implementing Feature and Bug Fixes 10 | 11 | We are welcome and greedy for PRs. So, 12 | 13 | * If you wanna fix a bug, simply submit it. 14 | * If you wanna implement feature or support with contributions, just drop a message to arunoda [at] kadira.io. 15 | 16 | 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 MeteorHacks Pvt Ltd (Sri Lanka). 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FineRouter 2 | 3 | 4 | Carefully Designed Client Side Router for Meteor. 5 | 6 | PLEASE NOTE: FineRouter is meant to continue where FlowRouter left off. All PRs will be considered. Also, contributor roles are given upon request. 7 | 8 | FineRouter is a very simple router for Meteor. It does routing for client-side apps and does not handle rendering itself. 9 | 10 | It exposes a great API for changing the URL and reactively getting data from the URL. However, inside the router, it's not reactive. Most importantly, FineRouter is designed with performance in mind and it focuses on what it does best: **routing**. 11 | 12 | ## TOC 13 | 14 | * [Meteor Routing Guide](#meteor-routing-guide) 15 | * [Getting Started](#getting-started) 16 | * [Routes Definition](#routes-definition) 17 | * [Group Routes](#group-routes) 18 | * [Rendering and Layout Management](#rendering-and-layout-management) 19 | * [Triggers](#triggers) 20 | * [Not Found Routes](#not-found-routes) 21 | * [API](#api) 22 | * [Subscription Management](#subscription-management) 23 | * [IE9 Support](#ie9-support) 24 | * [Hashbang URLs](#hashbang-urls) 25 | * [Prefixed paths](#prefixed-paths) 26 | * [Add-ons](#add-ons) 27 | * [Difference with Iron Router](#difference-with-iron-router) 28 | * [Migrating into 2.0](#migrating-into-20) 29 | 30 | ## Meteor Routing Guide 31 | 32 | [Meteor Routing Guide](https://kadira.io/academy/meteor-routing-guide) is a completed guide into **routing** and related topics in Meteor. It talks about how to use FineRouter properly and use it with **Blaze and React**. It also shows how to manage **subscriptions** and implement **auth logic** in the view layer. 33 | 34 | [![Meteor Routing Guide](https://cldup.com/AxlPfoxXmR.png)](https://kadira.io/academy/meteor-routing-guide) 35 | 36 | ## Getting Started 37 | 38 | Add FineRouter to your app: 39 | 40 | ~~~shell 41 | meteor add aadams:fine-router 42 | ~~~ 43 | 44 | Let's write our first route (add this file to `lib/router.js`): 45 | 46 | ~~~js 47 | FineRouter.route('/blog/:postId', { 48 | action: function(params, queryParams) { 49 | console.log("Yeah! We are on the post:", params.postId); 50 | } 51 | }); 52 | ~~~ 53 | 54 | Then visit `/blog/my-post-id` from the browser or invoke the following command from the browser console: 55 | 56 | ~~~js 57 | FineRouter.go('/blog/my-post-id'); 58 | ~~~ 59 | 60 | Then you can see some messages printed in the console. 61 | 62 | ## Routes Definition 63 | 64 | FineRouter routes are very simple and based on the syntax of [path-to-regexp](https://github.com/pillarjs/path-to-regexp) which is used in both [Express](http://expressjs.com/) and `iron:router`. 65 | 66 | Here's the syntax for a simple route: 67 | 68 | ~~~js 69 | FineRouter.route('/blog/:postId', { 70 | // do some action for this route 71 | action: function(params, queryParams) { 72 | console.log("Params:", params); 73 | console.log("Query Params:", queryParams); 74 | }, 75 | 76 | name: "" // optional 77 | }); 78 | ~~~ 79 | 80 | So, this route will be activated when you visit a url like below: 81 | 82 | ~~~js 83 | FineRouter.go('/blog/my-post?comments=on&color=dark'); 84 | ~~~ 85 | 86 | After you've visit the route, this will be printed in the console: 87 | 88 | ~~~ 89 | Params: {postId: "my-post"} 90 | Query Params: {comments: "on", color: "dark"} 91 | ~~~ 92 | 93 | For a single interaction, the router only runs once. That means, after you've visit a route, first it will call `triggers`, then `subscriptions` and finally `action`. After that happens, none of those methods will be called again for that route visit. 94 | 95 | You can define routes anywhere in the `client` directory. But, we recommend to add them in the `lib` directory. Then `fast-render` can detect subscriptions and send them for you (we'll talk about this is a moment). 96 | 97 | ### Group Routes 98 | 99 | You can group routes for better route organization. Here's an example: 100 | 101 | ~~~js 102 | var adminRoutes = FineRouter.group({ 103 | prefix: '/admin', 104 | name: 'admin', 105 | triggersEnter: [function(context, redirect) { 106 | console.log('running group triggers'); 107 | }] 108 | }); 109 | 110 | // handling /admin route 111 | adminRoutes.route('/', { 112 | action: function() { 113 | BlazeLayout.render('componentLayout', {content: 'admin'}); 114 | }, 115 | triggersEnter: [function(context, redirect) { 116 | console.log('running /admin trigger'); 117 | }] 118 | }); 119 | 120 | // handling /admin/posts 121 | adminRoutes.route('/posts', { 122 | action: function() { 123 | BlazeLayout.render('componentLayout', {content: 'posts'}); 124 | } 125 | }); 126 | ~~~ 127 | 128 | **All of the options for the `FineRouter.group()` are optional.** 129 | 130 | You can even have nested group routes as shown below: 131 | 132 | ~~~js 133 | var adminRoutes = FineRouter.group({ 134 | prefix: "/admin", 135 | name: "admin" 136 | }); 137 | 138 | var superAdminRoutes = adminRoutes.group({ 139 | prefix: "/super", 140 | name: "superadmin" 141 | }); 142 | 143 | // handling /admin/super/post 144 | superAdminRoutes.route('/post', { 145 | action: function() { 146 | 147 | } 148 | }); 149 | ~~~ 150 | 151 | You can determine which group the current route is in using: 152 | 153 | ~~~js 154 | FineRouter.current().route.group.name 155 | ~~~ 156 | 157 | This can be useful for determining if the current route is in a specific group (e.g. *admin*, *public*, *loggedIn*) without needing to use prefixes if you don't want to. If it's a nested group, you can get the parent group's name with: 158 | 159 | ~~~js 160 | FineRouter.current().route.group.parent.name 161 | ~~~ 162 | 163 | As with all current route properties, these are not reactive, but can be combined with `FineRouter.watchPathChange()` to get group names reactively. 164 | 165 | ## Rendering and Layout Management 166 | 167 | FineRouter does not handle rendering or layout management. For that, you can use: 168 | 169 | * [Blaze Layout for Blaze](https://github.com/kadirahq/blaze-layout) 170 | * [React Layout for React](https://github.com/kadirahq/meteor-react-layout) 171 | 172 | Then you can invoke the layout manager inside the `action` method in the router. 173 | 174 | ~~~js 175 | FineRouter.route('/blog/:postId', { 176 | action: function(params) { 177 | BlazeLayout.render("mainLayout", {area: "blog"}); 178 | } 179 | }); 180 | ~~~ 181 | 182 | ## Triggers 183 | 184 | Triggers are the way FineRouter allows you to perform tasks before you **enter** into a route and after you **exit** from a route. 185 | 186 | #### Defining triggers for a route 187 | 188 | Here's how you can define triggers for a route: 189 | 190 | ~~~js 191 | FineRouter.route('/home', { 192 | // calls just before the action 193 | triggersEnter: [trackRouteEntry], 194 | action: function() { 195 | // do something you like 196 | }, 197 | // calls when when we decide to move to another route 198 | // but calls before the next route started 199 | triggersExit: [trackRouteClose] 200 | }); 201 | 202 | function trackRouteEntry(context) { 203 | // context is the output of `FineRouter.current()` 204 | Mixpanel.track("visit-to-home", context.queryParams); 205 | } 206 | 207 | function trackRouteClose(context) { 208 | Mixpanel.track("move-from-home", context.queryParams); 209 | } 210 | ~~~ 211 | 212 | #### Defining triggers for a group route 213 | 214 | This is how you can define triggers on a group definition. 215 | 216 | ~~~js 217 | var adminRoutes = FineRouter.group({ 218 | prefix: '/admin', 219 | triggersEnter: [trackRouteEntry], 220 | triggersExit: [trackRouteEntry] 221 | }); 222 | ~~~ 223 | 224 | > You can add triggers to individual routes in the group too. 225 | 226 | #### Defining Triggers Globally 227 | 228 | You can also define triggers globally. Here's how to do it: 229 | 230 | ~~~js 231 | FineRouter.triggers.enter([cb1, cb2]); 232 | FineRouter.triggers.exit([cb1, cb2]); 233 | 234 | // filtering 235 | FineRouter.triggers.enter([trackRouteEntry], {only: ["home"]}); 236 | FineRouter.triggers.exit([trackRouteExit], {except: ["home"]}); 237 | ~~~ 238 | 239 | As you can see from the last two examples, you can filter routes using the `only` or `except` keywords. But, you can't use both `only` and `except` at once. 240 | 241 | > If you'd like to learn more about triggers and design decisions, visit [here](https://github.com/meteorhacks/flow-router/pull/59). 242 | 243 | #### Redirecting With Triggers 244 | 245 | You can redirect to a different route using triggers. You can do it from both enter and exit triggers. See how to do it: 246 | 247 | ~~~js 248 | FineRouter.route('/', { 249 | triggersEnter: [function(context, redirect) { 250 | redirect('/some-other-path'); 251 | }], 252 | action: function(_params) { 253 | throw new Error("this should not get called"); 254 | } 255 | }); 256 | ~~~ 257 | 258 | Every trigger callback comes with a second argument: a function you can use to redirect to a different route. Redirect also has few properties to make sure it's not blocking the router. 259 | 260 | * redirect must be called with an URL 261 | * redirect must be called within the same event loop cycle (no async or called inside a Tracker) 262 | * redirect cannot be called multiple times 263 | 264 | Check this [PR](https://github.com/meteorhacks/Fine-router/pull/172) to learn more about our redirect API. 265 | 266 | #### Stopping the Callback With Triggers 267 | 268 | In some cases, you may need to stop the route callback from firing using triggers. You can do this in **before** triggers, using the third argument: the `stop` function. For example, you can check the prefix and if it fails, show the notFound layout and stop before the action fires. 269 | 270 | ```js 271 | var localeGroup = FineRouter.group({ 272 | prefix: '/:locale?', 273 | triggersEnter: [localeCheck] 274 | }); 275 | 276 | localeGroup.route('/login', { 277 | action: function (params, queryParams) { 278 | BlazeLayout.render('componentLayout', {content: 'login'}); 279 | } 280 | }); 281 | 282 | function localeCheck(context, redirect, stop) { 283 | var locale = context.params.locale; 284 | 285 | if (locale !== undefined && locale !== 'fr') { 286 | BlazeLayout.render('notFound'); 287 | stop(); 288 | } 289 | } 290 | ``` 291 | 292 | > **Note**: When using the stop function, you should always pass the second **redirect** argument, even if you won't use it. 293 | 294 | ## Not Found Routes 295 | 296 | You can configure Not Found routes like this: 297 | 298 | ~~~js 299 | FineRouter.notFound = { 300 | // Subscriptions registered here don't have Fast Render support. 301 | subscriptions: function() { 302 | 303 | }, 304 | action: function() { 305 | 306 | } 307 | }; 308 | ~~~ 309 | 310 | ## API 311 | 312 | FineRouter has a rich API to help you to navigate the router and reactively get information from the router. 313 | 314 | #### FineRouter.getParam(paramName); 315 | 316 | Reactive function which you can use to get a parameter from the URL. 317 | 318 | ~~~js 319 | // route def: /apps/:appId 320 | // url: /apps/this-is-my-app 321 | 322 | var appId = FineRouter.getParam("appId"); 323 | console.log(appId); // prints "this-is-my-app" 324 | ~~~ 325 | 326 | #### FineRouter.getQueryParam(queryStringKey); 327 | 328 | Reactive function which you can use to get a value from the queryString. 329 | 330 | ~~~js 331 | // route def: /apps/:appId 332 | // url: /apps/this-is-my-app?show=yes&color=red 333 | 334 | var color = FineRouter.getQueryParam("color"); 335 | console.log(color); // prints "red" 336 | ~~~ 337 | 338 | #### FineRouter.path(pathDef, params, queryParams) 339 | 340 | Generate a path from a path definition. Both `params` and `queryParams` are optional. 341 | 342 | Special characters in `params` and `queryParams` will be URL encoded. 343 | 344 | ~~~js 345 | var pathDef = "/blog/:cat/:id"; 346 | var params = {cat: "met eor", id: "abc"}; 347 | var queryParams = {show: "y+e=s", color: "black"}; 348 | 349 | var path = FineRouter.path(pathDef, params, queryParams); 350 | console.log(path); // prints "/blog/met%20eor/abc?show=y%2Be%3Ds&color=black" 351 | ~~~ 352 | 353 | If there are no params or queryParams, this will simply return the pathDef as it is. 354 | 355 | ##### Using Route name instead of the pathDef 356 | 357 | You can also use the route's name instead of the pathDef. Then, FineRouter will pick the pathDef from the given route. See the following example: 358 | 359 | ~~~js 360 | FineRouter.route("/blog/:cat/:id", { 361 | name: "blogPostRoute", 362 | action: function(params) { 363 | //... 364 | } 365 | }) 366 | 367 | var params = {cat: "meteor", id: "abc"}; 368 | var queryParams = {show: "yes", color: "black"}; 369 | 370 | var path = FineRouter.path("blogPostRoute", params, queryParams); 371 | console.log(path); // prints "/blog/meteor/abc?show=yes&color=black" 372 | ~~~ 373 | 374 | #### FineRouter.go(pathDef, params, queryParams); 375 | 376 | This will get the path via `FineRouter.path` based on the arguments and re-route to that path. 377 | 378 | You can call `FineRouter.go` like this as well: 379 | 380 | ~~~js 381 | FineRouter.go("/blog"); 382 | ~~~ 383 | 384 | 385 | #### FineRouter.url(pathDef, params, queryParams) 386 | 387 | Just like `FineRouter.path`, but gives the absolute url. (Uses `Meteor.absoluteUrl` behind the scenes.) 388 | 389 | #### FineRouter.setParams(newParams) 390 | 391 | This will change the current params with the newParams and re-route to the new path. 392 | 393 | ~~~js 394 | // route def: /apps/:appId 395 | // url: /apps/this-is-my-app?show=yes&color=red 396 | 397 | FineRouter.setParams({appId: "new-id"}); 398 | // Then the user will be redirected to the following path 399 | // /apps/new-id?show=yes&color=red 400 | ~~~ 401 | 402 | #### FineRouter.setQueryParams(newQueryParams) 403 | 404 | Just like `FineRouter.setParams`, but for queryString params. 405 | 406 | To remove a query param set it to `null` like below: 407 | 408 | ~~~js 409 | FineRouter.setQueryParams({paramToRemove: null}); 410 | ~~~ 411 | 412 | #### FineRouter.getRouteName() 413 | 414 | To get the name of the route reactively. 415 | 416 | ~~~js 417 | Tracker.autorun(function() { 418 | var routeName = FineRouter.getRouteName(); 419 | console.log("Current route name is: ", routeName); 420 | }); 421 | ~~~ 422 | 423 | #### FineRouter.current() 424 | 425 | Get the current state of the router. **This API is not reactive**. 426 | If you need to watch the changes in the path simply use `FineRouter.watchPathChange()`. 427 | 428 | This gives an object like this: 429 | 430 | ~~~js 431 | // route def: /apps/:appId 432 | // url: /apps/this-is-my-app?show=yes&color=red 433 | 434 | var current = FineRouter.current(); 435 | console.log(current); 436 | 437 | // prints following object 438 | // { 439 | // path: "/apps/this-is-my-app?show=yes&color=red", 440 | // params: {appId: "this-is-my-app"}, 441 | // queryParams: {show: "yes", color: "red"} 442 | // route: {pathDef: "/apps/:appId", name: "name-of-the-route"} 443 | // } 444 | ~~~ 445 | 446 | #### FineRouter.watchPathChange() 447 | 448 | Reactively watch the changes in the path. If you need to simply get the params or queryParams use dedicated APIs like `FineRouter.getQueryParam()`. 449 | 450 | ~~~js 451 | Tracker.autorun(function() { 452 | FineRouter.watchPathChange(); 453 | var currentContext = FineRouter.current(); 454 | // do anything with the current context 455 | // or anything you wish 456 | }); 457 | ~~~ 458 | 459 | #### FineRouter.withReplaceState(fn) 460 | Normally, all the route changes made via APIs like `FineRouter.go` and `FineRouter.setParams()` add a URL item to the browser history. For example, run the following code: 461 | 462 | ~~~js 463 | FineRouter.setParams({id: "the-id-1"}); 464 | FineRouter.setParams({id: "the-id-2"}); 465 | FineRouter.setParams({id: "the-id-3"}); 466 | ~~~ 467 | 468 | Now you can hit the back button of your browser two times. This is normal behavior since users may click the back button and expect to see the previous state of the app. 469 | 470 | But sometimes, this is not something you want. You don't need to pollute the browser history. Then, you can use the following syntax. 471 | 472 | ~~~js 473 | FineRouter.withReplaceState(function() { 474 | FineRouter.setParams({id: "the-id-1"}); 475 | FineRouter.setParams({id: "the-id-2"}); 476 | FineRouter.setParams({id: "the-id-3"}); 477 | }); 478 | ~~~ 479 | 480 | Now, there is no item in the browser history. Just like `FineRouter.setParams`, you can use any FineRouter API inside `FineRouter.withReplaceState`. 481 | 482 | > We named this function as `withReplaceState` because, replaceState is the underline API used for this functionality. Read more about [replace state & the history API](https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history). 483 | 484 | #### FineRouter.reload() 485 | 486 | FineRouter routes are idempotent. That means, even if you call `FineRouter.go()` to the same URL multiple times, it only activates in the first run. This is also true for directly clicking on paths. 487 | 488 | So, if you really need to reload the route, this is the API you want. 489 | 490 | #### FineRouter.wait() and FineRouter.initialize() 491 | 492 | By default, FineRouter initializes the routing process in a `Meteor.startup()` callback. This works for most of the apps. But, some apps have custom initializations and FineRouter needs to initialize after that. 493 | 494 | So, that's where `FineRouter.wait()` comes to save you. You need to call it directly inside your JavaScript file. After that, whenever your app is ready call `FineRouter.initialize()`. 495 | 496 | eg:- 497 | 498 | ~~~js 499 | // file: app.js 500 | FineRouter.wait(); 501 | WhenEverYourAppIsReady(function() { 502 | FineRouter.initialize(); 503 | }); 504 | ~~~ 505 | 506 | For more information visit [issue #180](https://github.com/meteorhacks/flow-router/issues/180). 507 | 508 | #### FineRouter.onRouteRegister(cb) 509 | 510 | This API is specially designed for add-on developers. They can listen for any registered route and add custom functionality to FineRouter. This works on both server and client alike. 511 | 512 | ~~~js 513 | FineRouter.onRouteRegister(function(route) { 514 | // do anything with the route object 515 | console.log(route); 516 | }); 517 | ~~~ 518 | 519 | Let's say a user defined a route like this: 520 | 521 | ~~~js 522 | FineRouter.route('/blog/:post', { 523 | name: 'postList', 524 | triggersEnter: [function() {}], 525 | subscriptions: function() {}, 526 | action: function() {}, 527 | triggersExit: [function() {}], 528 | customField: 'customName' 529 | }); 530 | ~~~ 531 | 532 | Then the route object will be something like this: 533 | 534 | ~~~js 535 | { 536 | pathDef: '/blog/:post', 537 | name: 'postList', 538 | options: {customField: 'customName'} 539 | } 540 | ~~~ 541 | 542 | So, it's not the internal route object we are using. 543 | 544 | ## Subscription Management 545 | 546 | For Subscription Management, we highly suggest you to follow [Template/Component level subscriptions](https://kadira.io/academy/meteor-routing-guide/content/subscriptions-and-data-management). Visit this [guide](https://kadira.io/academy/meteor-routing-guide/content/subscriptions-and-data-management) for that. 547 | 548 | FineRouter also has it's own subscription registration mechanism. We will remove this in version 3.0. We don't remove or deprecate it in version 2.x because this is the easiest way to implement FastRender support for your app. In 3.0 we've better support for FastRender with Server Side Rendering. 549 | 550 | FineRouter only deals with registration of subscriptions. It does not wait until subscription becomes ready. This is how to register a subscription. 551 | 552 | ~~~js 553 | FineRouter.route('/blog/:postId', { 554 | subscriptions: function(params, queryParams) { 555 | this.register('myPost', Meteor.subscribe('blogPost', params.postId)); 556 | } 557 | }); 558 | ~~~ 559 | 560 | We can also register global subscriptions like this: 561 | 562 | ~~~js 563 | FineRouter.subscriptions = function() { 564 | this.register('myCourses', Meteor.subscribe('courses')); 565 | }; 566 | ~~~ 567 | 568 | All these global subscriptions run on every route. So, pay special attention to names when registering subscriptions. 569 | 570 | After you've registered your subscriptions, you can reactively check for the status of those subscriptions like this: 571 | 572 | ~~~js 573 | Tracker.autorun(function() { 574 | console.log("Is myPost ready?:", FineRouter.subsReady("myPost")); 575 | console.log("Are all subscriptions ready?:", FineRouter.subsReady()); 576 | }); 577 | ~~~ 578 | 579 | So, you can use `FineRouter.subsReady` inside template helpers to show the loading status and act accordingly. 580 | 581 | ### FineRouter.subsReady() with a callback 582 | 583 | Sometimes, we need to use `FineRouter.subsReady()` in places where an autorun is not available. One such example is inside an event handler. For such places, we can use the callback API of `FineRouter.subsReady()`. 584 | 585 | ~~~js 586 | Template.myTemplate.events({ 587 | "click #id": function(){ 588 | FineRouter.subsReady("myPost", function() { 589 | // do something 590 | }); 591 | } 592 | }); 593 | ~~~ 594 | 595 | > Arunoda has discussed more about Subscription Management in FineRouter in [this](https://meteorhacks.com/flow-router-and-subscription-management.html#subscription-management) blog post about [FineRouter and Subscription Management](https://meteorhacks.com/flow-router-and-subscription-management.html). 596 | 597 | > He's showing how to build an app like this: 598 | 599 | >![FineRouter's Subscription Management](https://cldup.com/esLzM8cjEL.gif) 600 | 601 | #### Fast Render 602 | FineRouter has built in support for [Fast Render](https://github.com/meteorhacks/fast-render). 603 | 604 | - `meteor add meteorhacks:fast-render` 605 | - Put `router.js` in a shared location. We suggest `lib/router.js`. 606 | 607 | You can exclude Fast Render support by wrapping the subscription registration in an `isClient` block: 608 | 609 | ~~~js 610 | FineRouter.route('/blog/:postId', { 611 | subscriptions: function(params, queryParams) { 612 | // using Fast Render 613 | this.register('myPost', Meteor.subscribe('blogPost', params.postId)); 614 | 615 | // not using Fast Render 616 | if(Meteor.isClient) { 617 | this.register('data', Meteor.subscribe('bootstrap-data'); 618 | } 619 | } 620 | }); 621 | ~~~ 622 | 623 | #### Subscription Caching 624 | 625 | You can also use [Subs Manager](https://github.com/meteorhacks/subs-manager) for caching subscriptions on the client. We haven't done anything special to make it work. It should work as it works with other routers. 626 | 627 | ## IE9 Support 628 | 629 | FineRouter has IE9 support. But it does not ship the **HTML5 history polyfill** out of the box. That's because most apps do not require it. 630 | 631 | If you need to support IE9, add the **HTML5 history polyfill** with the following package. 632 | 633 | ~~~shell 634 | meteor add tomwasd:history-polyfill 635 | ~~~ 636 | 637 | ## Hashbang URLs 638 | 639 | To enable hashbang urls like `mydomain.com/#!/mypath` simple set the `hashbang` option to `true` in the initialize function: 640 | 641 | ~~~js 642 | // file: app.js 643 | FineRouter.wait(); 644 | WhenEverYourAppIsReady(function() { 645 | FineRouter.initialize({hashbang: true}); 646 | }); 647 | ~~~ 648 | 649 | ## Prefixed paths 650 | 651 | In cases you wish to run multiple web application on the same domain name, you’ll probably want to serve your particular meteor application under a sub-path (eg `example.com/myapp`). In this case simply include the path prefix in the meteor `ROOT_URL` environment variable and FineRouter will handle it transparently without any additional configuration. 652 | 653 | ## Add-ons 654 | 655 | Router is a base package for an app. Other projects like [useraccounts](http://useraccounts.meteor.com/) should have support for FineRouter. Otherwise, it's hard to use FineRouter in a real project. Now a lot of packages have [started to support FineRouter](https://kadira.io/blog/meteor/addon-packages-for-flowrouter). 656 | 657 | So, you can use your your favorite package with FineRouter as well. If not, there is an [easy process](https://kadira.io/blog/meteor/addon-packages-for-flowrouter#what-if-project-xxx-still-doesn-t-support-flowrouter-) to convert them to FineRouter. 658 | 659 | **Add-on API** 660 | 661 | We have also released a [new API](https://github.com/kadirahq/flow-router#flowrouteronrouteregistercb) to support add-on developers. With that add-on packages can get a notification, when the user created a route in their app. 662 | 663 | If you've more ideas for the add-on API, [let us know](https://github.com/kadirahq/flow-router/issues). 664 | 665 | ## Difference with Iron Router 666 | 667 | FineRouter and Iron Router are two different routers. Iron Router tries to be a full featured solution. It tries to do everything including routing, subscriptions, rendering and layout management. 668 | 669 | FineRouter is a minimalistic solution focused on routing with UI performance in mind. It exposes APIs for related functionality. 670 | 671 | Let's learn more about the differences: 672 | 673 | ### Rendering 674 | 675 | FineRouter doesn't handle rendering. By decoupling rendering from the router it's possible to use any rendering framework, such as [Blaze Layout](https://github.com/kadirahq/blaze-layout) to render with Blaze's Dynamic Templates. Rendering calls are made in the the route's action. We have a layout manager for [React](https://github.com/kadirahq/meteor-react-layout) as well. 676 | 677 | ### Subscriptions 678 | 679 | With FineRouter, we highly suggest using template/component layer subscriptions. But, if you need to do routing in the router layer, FineRouter has [subscription registration](#subscription-management) mechanism. Even with that, FineRouter never waits for the subscriptions and view layer to do it. 680 | 681 | ### Reactive Content 682 | 683 | In Iron Router you can use reactive content inside the router, but any hook or method can re-run in an unpredictable manner. FineRouter limits reactive data sources to a single run; when it is first called. 684 | 685 | We think that's the way to go. Router is just a user action. We can work with reactive content in the rendering layer. 686 | 687 | ### router.current() is evil 688 | 689 | `Router.current()` is evil. Why? Let's look at following example. Imagine we have a route like this in our app: 690 | 691 | ~~~ 692 | /apps/:appId/:section 693 | ~~~ 694 | 695 | Now let's say, we need to get `appId` from the URL. Then we will do, something like this in Iron Router. 696 | 697 | ~~~js 698 | Templates['foo'].helpers({ 699 | "someData": function() { 700 | var appId = Router.current().params.appId; 701 | return doSomething(appId); 702 | } 703 | }); 704 | ~~~ 705 | 706 | Let's say we changed `:section` in the route. Then the above helper also gets rerun. If we add a query param to the URL, it gets rerun. That's because `Router.current()` looks for changes in the route(or URL). But in any of above cases, `appId` didn't get changed. 707 | 708 | Because of this, a lot parts of our app get re-run and re-rendered. This creates unpredictable rendering behavior in our app. 709 | 710 | FineRouter fixes this issue by providing the `Router.getParam()` API. See how to use it: 711 | 712 | ~~~js 713 | Templates['foo'].helpers({ 714 | "someData": function() { 715 | var appId = FineRouter.getParam('appId'); 716 | return doSomething(appId); 717 | } 718 | }); 719 | ~~~ 720 | 721 | ### No data context 722 | 723 | FineRouter does not have a data context. Data context has the same problem as reactive `.current()`. We believe, it'll possible to get data directly in the template (component) layer. 724 | 725 | ### Built in Fast Render Support 726 | 727 | FineRouter has built in [Fast Render](https://github.com/meteorhacks/fast-render) support. Just add Fast Render to your app and it'll work. Nothing to change in the router. 728 | 729 | For more information check [docs](#fast-render). 730 | 731 | ### Server Side Routing 732 | 733 | FineRouter is a client side router and it **does not** support server side routing at all. But `subscriptions` run on the server to enable Fast Render support. 734 | 735 | #### Reason behind that 736 | 737 | Meteor is not a traditional framework where you can send HTML directly from the server. Meteor needs to send a special set of HTML to the client initially. So, you can't directly send something to the client yourself. 738 | 739 | Also, in the server we need look for different things compared with the client. For example: 740 | 741 | * In the server we have to deal with headers. 742 | * In the server we have to deal with methods like `GET`, `POST`, etc. 743 | * In the server we have Cookies. 744 | 745 | So, it's better to use a dedicated server-side router like [`meteorhacks:picker`](https://github.com/meteorhacks/picker). It supports connect and express middlewares and has a very easy to use route syntax. 746 | 747 | ### Server Side Rendering 748 | 749 | FineRouter 3.0 will have server side rendering support. We've already started the initial version and check our [`ssr`](https://github.com/meteorhacks/flow-router/tree/ssr) branch for that. 750 | 751 | It's currently very usable and Kadira already using it for 752 | 753 | ### Better Initial Loading Support 754 | 755 | In Meteor, we have to wait until all the JS and other resources send before rendering anything. This is an issue. In 3.0, with the support from Server Side Rendering we are going to fix it. 756 | 757 | ## Migrating into 2.0 758 | 759 | Migrating into version 2.0 is easy and you don't need to change any application code since you are already using 2.0 features and the APIs. In 2.0, we've changed names and removed some deprecated APIs. 760 | 761 | Here are the steps to migrate your app into 2.0. 762 | 763 | #### Use the New FineRouter Package 764 | * Now FlowRouter comes as `aadams:fine-router` 765 | * So, remove `meteorhacks:flow-router` with : `meteor remove meteorhacks:flow-router` 766 | * Then, add `aadams:fine-router` with `meteor add aadams:fine-router` 767 | 768 | #### Change FlowLayout into BlazeLayout 769 | * We've also renamed FlowLayout as [BlazeLayout](https://github.com/kadirahq/blaze-layout). 770 | * So, remove `meteorhacks:flow-layout` and add `kadira:blaze-layout` instead. 771 | * You need to use `BlazeLayout.render()` instead of `FlowLayout.render()` 772 | 773 | #### Stop using deprecated Apis 774 | * There is no middleware support. Use triggers instead. 775 | * There is no API called `.reactiveCurrent()`, use `.watchPathChange()` instead. 776 | * Earlier, you can access query params with `FlowRouter.current().params.query`. But, now you can't do that. Use `FlowRouter.current().queryParams` instead. 777 | -------------------------------------------------------------------------------- /client/_init.js: -------------------------------------------------------------------------------- 1 | // Export Router Instance 2 | FineRouter = new Router(); 3 | FineRouter.Router = Router; 4 | FineRouter.Route = Route; 5 | 6 | // Initialize FineRouter 7 | Meteor.startup(function () { 8 | if(!FineRouter._askedToWait) { 9 | FineRouter.initialize(); 10 | } 11 | }); 12 | -------------------------------------------------------------------------------- /client/group.js: -------------------------------------------------------------------------------- 1 | Group = function(router, options, parent) { 2 | options = options || {}; 3 | 4 | if (options.prefix && !/^\/.*/.test(options.prefix)) { 5 | var message = "group's prefix must start with '/'"; 6 | throw new Error(message); 7 | } 8 | 9 | this._router = router; 10 | this.prefix = options.prefix || ''; 11 | this.name = options.name; 12 | this.options = options; 13 | 14 | this._triggersEnter = options.triggersEnter || []; 15 | this._triggersExit = options.triggersExit || []; 16 | this._subscriptions = options.subscriptions || Function.prototype; 17 | 18 | this.parent = parent; 19 | if (this.parent) { 20 | this.prefix = parent.prefix + this.prefix; 21 | 22 | this._triggersEnter = parent._triggersEnter.concat(this._triggersEnter); 23 | this._triggersExit = this._triggersExit.concat(parent._triggersExit); 24 | } 25 | }; 26 | 27 | Group.prototype.route = function(pathDef, options, group) { 28 | options = options || {}; 29 | 30 | if (!/^\/.*/.test(pathDef)) { 31 | var message = "route's path must start with '/'"; 32 | throw new Error(message); 33 | } 34 | 35 | group = group || this; 36 | pathDef = this.prefix + pathDef; 37 | 38 | var triggersEnter = options.triggersEnter || []; 39 | options.triggersEnter = this._triggersEnter.concat(triggersEnter); 40 | 41 | var triggersExit = options.triggersExit || []; 42 | options.triggersExit = triggersExit.concat(this._triggersExit); 43 | 44 | return this._router.route(pathDef, options, group); 45 | }; 46 | 47 | Group.prototype.group = function(options) { 48 | return new Group(this._router, options, this); 49 | }; 50 | 51 | Group.prototype.callSubscriptions = function(current) { 52 | if (this.parent) { 53 | this.parent.callSubscriptions(current); 54 | } 55 | 56 | this._subscriptions.call(current.route, current.params, current.queryParams); 57 | }; 58 | -------------------------------------------------------------------------------- /client/modules.js: -------------------------------------------------------------------------------- 1 | page = require('page'); 2 | qs = require('qs'); 3 | -------------------------------------------------------------------------------- /client/route.js: -------------------------------------------------------------------------------- 1 | Route = function(router, pathDef, options, group) { 2 | options = options || {}; 3 | 4 | this.options = options; 5 | this.pathDef = pathDef 6 | 7 | // Route.path is deprecated and will be removed in 3.0 8 | this.path = pathDef; 9 | 10 | if (options.name) { 11 | this.name = options.name; 12 | } 13 | 14 | this._action = options.action || Function.prototype; 15 | this._subscriptions = options.subscriptions || Function.prototype; 16 | this._triggersEnter = options.triggersEnter || []; 17 | this._triggersExit = options.triggersExit || []; 18 | this._subsMap = {}; 19 | this._router = router; 20 | 21 | this._params = new ReactiveDict(); 22 | this._queryParams = new ReactiveDict(); 23 | this._routeCloseDep = new Tracker.Dependency(); 24 | 25 | // tracks the changes in the URL 26 | this._pathChangeDep = new Tracker.Dependency(); 27 | 28 | this.group = group; 29 | }; 30 | 31 | Route.prototype.clearSubscriptions = function() { 32 | this._subsMap = {}; 33 | }; 34 | 35 | Route.prototype.register = function(name, sub, options) { 36 | this._subsMap[name] = sub; 37 | }; 38 | 39 | 40 | Route.prototype.getSubscription = function(name) { 41 | return this._subsMap[name]; 42 | }; 43 | 44 | 45 | Route.prototype.getAllSubscriptions = function() { 46 | return this._subsMap; 47 | }; 48 | 49 | Route.prototype.callAction = function(current) { 50 | var self = this; 51 | self._action(current.params, current.queryParams); 52 | }; 53 | 54 | Route.prototype.callSubscriptions = function(current) { 55 | this.clearSubscriptions(); 56 | if (this.group) { 57 | this.group.callSubscriptions(current); 58 | } 59 | 60 | this._subscriptions(current.params, current.queryParams); 61 | }; 62 | 63 | Route.prototype.getRouteName = function() { 64 | this._routeCloseDep.depend(); 65 | return this.name; 66 | }; 67 | 68 | Route.prototype.getParam = function(key) { 69 | this._routeCloseDep.depend(); 70 | return this._params.get(key); 71 | }; 72 | 73 | Route.prototype.getQueryParam = function(key) { 74 | this._routeCloseDep.depend(); 75 | return this._queryParams.get(key); 76 | }; 77 | 78 | Route.prototype.watchPathChange = function() { 79 | this._pathChangeDep.depend(); 80 | }; 81 | 82 | Route.prototype.registerRouteClose = function() { 83 | this._params = new ReactiveDict(); 84 | this._queryParams = new ReactiveDict(); 85 | this._routeCloseDep.changed(); 86 | this._pathChangeDep.changed(); 87 | }; 88 | 89 | Route.prototype.registerRouteChange = function(currentContext, routeChanging) { 90 | // register params 91 | var params = currentContext.params; 92 | this._updateReactiveDict(this._params, params); 93 | 94 | // register query params 95 | var queryParams = currentContext.queryParams; 96 | this._updateReactiveDict(this._queryParams, queryParams); 97 | 98 | // if the route is changing, we need to defer triggering path changing 99 | // if we did this, old route's path watchers will detect this 100 | // Real issue is, above watcher will get removed with the new route 101 | // So, we don't need to trigger it now 102 | // We are doing it on the route close event. So, if they exists they'll 103 | // get notify that 104 | if(!routeChanging) { 105 | this._pathChangeDep.changed(); 106 | } 107 | }; 108 | 109 | Route.prototype._updateReactiveDict = function(dict, newValues) { 110 | var currentKeys = _.keys(newValues); 111 | var oldKeys = _.keys(dict.keyDeps); 112 | 113 | // set new values 114 | // params is an array. So, _.each(params) does not works 115 | // to iterate params 116 | _.each(currentKeys, function(key) { 117 | dict.set(key, newValues[key]); 118 | }); 119 | 120 | // remove keys which does not exisits here 121 | var removedKeys = _.difference(oldKeys, currentKeys); 122 | _.each(removedKeys, function(key) { 123 | dict.set(key, undefined); 124 | }); 125 | }; 126 | -------------------------------------------------------------------------------- /client/router.js: -------------------------------------------------------------------------------- 1 | Router = function () { 2 | var self = this; 3 | this.globals = []; 4 | this.subscriptions = Function.prototype; 5 | 6 | this._tracker = this._buildTracker(); 7 | this._current = {}; 8 | 9 | // tracks the current path change 10 | this._onEveryPath = new Tracker.Dependency(); 11 | 12 | this._globalRoute = new Route(this); 13 | 14 | // holds onRoute callbacks 15 | this._onRouteCallbacks = []; 16 | 17 | // if _askedToWait is true. We don't automatically start the router 18 | // in Meteor.startup callback. (see client/_init.js) 19 | // Instead user need to call `.initialize() 20 | this._askedToWait = false; 21 | this._initialized = false; 22 | this._triggersEnter = []; 23 | this._triggersExit = []; 24 | this._routes = []; 25 | this._routesMap = {}; 26 | this._updateCallbacks(); 27 | this.notFound = this.notfound = null; 28 | // indicate it's okay (or not okay) to run the tracker 29 | // when doing subscriptions 30 | // using a number and increment it help us to support FineRouter.go() 31 | // and legitimate reruns inside tracker on the same event loop. 32 | // this is a solution for #145 33 | this.safeToRun = 0; 34 | 35 | // Meteor exposes to the client the path prefix that was defined using the 36 | // ROOT_URL environement variable on the server using the global runtime 37 | // configuration. See #315. 38 | this._basePath = __meteor_runtime_config__.ROOT_URL_PATH_PREFIX || ''; 39 | 40 | // this is a chain contains a list of old routes 41 | // most of the time, there is only one old route 42 | // but when it's the time for a trigger redirect we've a chain 43 | this._oldRouteChain = []; 44 | 45 | this.env = { 46 | replaceState: new Meteor.EnvironmentVariable(), 47 | reload: new Meteor.EnvironmentVariable(), 48 | trailingSlash: new Meteor.EnvironmentVariable() 49 | }; 50 | 51 | // redirect function used inside triggers 52 | this._redirectFn = function(pathDef, fields, queryParams) { 53 | if (/^http(s)?:\/\//.test(pathDef)) { 54 | var message = "Redirects to URLs outside of the app are not supported in this version of Fine Router. Use 'window.location = yourUrl' instead"; 55 | throw new Error(message); 56 | } 57 | self.withReplaceState(function() { 58 | var path = FineRouter.path(pathDef, fields, queryParams); 59 | self._page.redirect(path); 60 | }); 61 | }; 62 | this._initTriggersAPI(); 63 | }; 64 | 65 | Router.prototype.route = function(pathDef, options, group) { 66 | if (!/^\/.*/.test(pathDef)) { 67 | var message = "route's path must start with '/'"; 68 | throw new Error(message); 69 | } 70 | 71 | options = options || {}; 72 | var self = this; 73 | var route = new Route(this, pathDef, options, group); 74 | 75 | // calls when the page route being activates 76 | route._actionHandle = function (context, next) { 77 | var oldRoute = self._current.route; 78 | self._oldRouteChain.push(oldRoute); 79 | 80 | var queryParams = self._qs.parse(context.querystring); 81 | // _qs.parse() gives us a object without prototypes, 82 | // created with Object.create(null) 83 | // Meteor's check doesn't play nice with it. 84 | // So, we need to fix it by cloning it. 85 | // see more: https://github.com/meteorhacks/flow-router/issues/164 86 | queryParams = JSON.parse(JSON.stringify(queryParams)); 87 | 88 | self._current = { 89 | path: context.path, 90 | context: context, 91 | params: context.params, 92 | queryParams: queryParams, 93 | route: route, 94 | oldRoute: oldRoute 95 | }; 96 | 97 | // we need to invalidate if all the triggers have been completed 98 | // if not that means, we've been redirected to another path 99 | // then we don't need to invalidate 100 | var afterAllTriggersRan = function() { 101 | self._invalidateTracker(); 102 | }; 103 | 104 | var triggers = self._triggersEnter.concat(route._triggersEnter); 105 | Triggers.runTriggers( 106 | triggers, 107 | self._current, 108 | self._redirectFn, 109 | afterAllTriggersRan 110 | ); 111 | }; 112 | 113 | // calls when you exit from the page js route 114 | route._exitHandle = function(context, next) { 115 | var triggers = self._triggersExit.concat(route._triggersExit); 116 | Triggers.runTriggers( 117 | triggers, 118 | self._current, 119 | self._redirectFn, 120 | next 121 | ); 122 | }; 123 | 124 | this._routes.push(route); 125 | if (options.name) { 126 | this._routesMap[options.name] = route; 127 | } 128 | 129 | this._updateCallbacks(); 130 | this._triggerRouteRegister(route); 131 | 132 | return route; 133 | }; 134 | 135 | Router.prototype.group = function(options) { 136 | return new Group(this, options); 137 | }; 138 | 139 | Router.prototype.path = function(pathDef, fields, queryParams) { 140 | if (this._routesMap[pathDef]) { 141 | pathDef = this._routesMap[pathDef].pathDef; 142 | } 143 | 144 | var path = ""; 145 | 146 | // Prefix the path with the router global prefix 147 | if (this._basePath) { 148 | path += "/" + this._basePath + "/"; 149 | } 150 | 151 | fields = fields || {}; 152 | var regExp = /(:[\w\(\)\\\+\*\.\?]+)+/g; 153 | path += pathDef.replace(regExp, function(key) { 154 | var firstRegexpChar = key.indexOf("("); 155 | // get the content behind : and (\\d+/) 156 | key = key.substring(1, (firstRegexpChar > 0)? firstRegexpChar: undefined); 157 | // remove +?* 158 | key = key.replace(/[\+\*\?]+/g, ""); 159 | 160 | // this is to allow page js to keep the custom characters as it is 161 | // we need to encode 2 times otherwise "/" char does not work properly 162 | // So, in that case, when I includes "/" it will think it's a part of the 163 | // route. encoding 2times fixes it 164 | return encodeURIComponent(encodeURIComponent(fields[key] || "")); 165 | }); 166 | 167 | // Replace multiple slashes with single slash 168 | path = path.replace(/\/\/+/g, "/"); 169 | 170 | // remove trailing slash 171 | // but keep the root slash if it's the only one 172 | path = path.match(/^\/{1}$/) ? path: path.replace(/\/$/, ""); 173 | 174 | // explictly asked to add a trailing slash 175 | if(this.env.trailingSlash.get() && _.last(path) !== "/") { 176 | path += "/"; 177 | } 178 | 179 | var strQueryParams = this._qs.stringify(queryParams || {}); 180 | if(strQueryParams) { 181 | path += "?" + strQueryParams; 182 | } 183 | 184 | return path; 185 | }; 186 | 187 | Router.prototype.go = function(pathDef, fields, queryParams) { 188 | var path = this.path(pathDef, fields, queryParams); 189 | 190 | var useReplaceState = this.env.replaceState.get(); 191 | if(useReplaceState) { 192 | this._page.replace(path); 193 | } else { 194 | this._page(path); 195 | } 196 | }; 197 | 198 | Router.prototype.reload = function() { 199 | var self = this; 200 | 201 | self.env.reload.withValue(true, function() { 202 | self._page.replace(self._current.path); 203 | }); 204 | }; 205 | 206 | Router.prototype.redirect = function(path) { 207 | this._page.redirect(path); 208 | }; 209 | 210 | Router.prototype.setParams = function(newParams) { 211 | if(!this._current.route) {return false;} 212 | 213 | var pathDef = this._current.route.pathDef; 214 | var existingParams = this._current.params; 215 | var params = {}; 216 | _.each(_.keys(existingParams), function(key) { 217 | params[key] = existingParams[key]; 218 | }); 219 | 220 | params = _.extend(params, newParams); 221 | var queryParams = this._current.queryParams; 222 | 223 | this.go(pathDef, params, queryParams); 224 | return true; 225 | }; 226 | 227 | Router.prototype.setQueryParams = function(newParams) { 228 | if(!this._current.route) {return false;} 229 | 230 | var queryParams = _.clone(this._current.queryParams); 231 | _.extend(queryParams, newParams); 232 | 233 | for (var k in queryParams) { 234 | if (queryParams[k] === null || queryParams[k] === undefined) { 235 | delete queryParams[k]; 236 | } 237 | } 238 | 239 | var pathDef = this._current.route.pathDef; 240 | var params = this._current.params; 241 | this.go(pathDef, params, queryParams); 242 | return true; 243 | }; 244 | 245 | // .current is not reactive 246 | // This is by design. use .getParam() instead 247 | // If you really need to watch the path change, use .watchPathChange() 248 | Router.prototype.current = function() { 249 | // We can't trust outside, that's why we clone this 250 | // Anyway, we can't clone the whole object since it has non-jsonable values 251 | // That's why we clone what's really needed. 252 | var current = _.clone(this._current); 253 | current.queryParams = EJSON.clone(current.queryParams); 254 | current.params = EJSON.clone(current.params); 255 | return current; 256 | }; 257 | 258 | // Implementing Reactive APIs 259 | var reactiveApis = [ 260 | 'getParam', 'getQueryParam', 261 | 'getRouteName', 'watchPathChange' 262 | ]; 263 | reactiveApis.forEach(function(api) { 264 | Router.prototype[api] = function(arg1) { 265 | // when this is calling, there may not be any route initiated 266 | // so we need to handle it 267 | var currentRoute = this._current.route; 268 | if(!currentRoute) { 269 | this._onEveryPath.depend(); 270 | return; 271 | } 272 | 273 | // currently, there is only one argument. If we've more let's add more args 274 | // this is not clean code, but better in performance 275 | return currentRoute[api].call(currentRoute, arg1); 276 | }; 277 | }); 278 | 279 | Router.prototype.subsReady = function() { 280 | var callback = null; 281 | var args = _.toArray(arguments); 282 | 283 | if (typeof _.last(args) === "function") { 284 | callback = args.pop(); 285 | } 286 | 287 | var currentRoute = this.current().route; 288 | var globalRoute = this._globalRoute; 289 | 290 | // we need to depend for every route change and 291 | // rerun subscriptions to check the ready state 292 | this._onEveryPath.depend(); 293 | 294 | if(!currentRoute) { 295 | return false; 296 | } 297 | 298 | var subscriptions; 299 | if(args.length === 0) { 300 | subscriptions = _.values(globalRoute.getAllSubscriptions()); 301 | subscriptions = subscriptions.concat(_.values(currentRoute.getAllSubscriptions())); 302 | } else { 303 | subscriptions = _.map(args, function(subName) { 304 | return globalRoute.getSubscription(subName) || currentRoute.getSubscription(subName); 305 | }); 306 | } 307 | 308 | var isReady = function() { 309 | var ready = _.every(subscriptions, function(sub) { 310 | return sub && sub.ready(); 311 | }); 312 | 313 | return ready; 314 | }; 315 | 316 | if (callback) { 317 | Tracker.autorun(function(c) { 318 | if (isReady()) { 319 | callback(); 320 | c.stop(); 321 | } 322 | }); 323 | } else { 324 | return isReady(); 325 | } 326 | }; 327 | 328 | Router.prototype.withReplaceState = function(fn) { 329 | return this.env.replaceState.withValue(true, fn); 330 | }; 331 | 332 | Router.prototype.withTrailingSlash = function(fn) { 333 | return this.env.trailingSlash.withValue(true, fn); 334 | }; 335 | 336 | Router.prototype._notfoundRoute = function(context) { 337 | this._current = { 338 | path: context.path, 339 | context: context, 340 | params: [], 341 | queryParams: {}, 342 | }; 343 | 344 | // XXX this.notfound kept for backwards compatibility 345 | this.notFound = this.notFound || this.notfound; 346 | if(!this.notFound) { 347 | if(!Meteor.isTest && !Meteor.isPackageTest && !Meteor.isAppTest) { 348 | console.error("There is no route for the path:", context.path); 349 | } 350 | return; 351 | } 352 | 353 | this._current.route = new Route(this, "*", this.notFound); 354 | this._invalidateTracker(); 355 | }; 356 | 357 | Router.prototype.initialize = function(options) { 358 | options = options || {}; 359 | 360 | if(this._initialized) { 361 | throw new Error("FineRouter is already initialized"); 362 | } 363 | 364 | var self = this; 365 | this._updateCallbacks(); 366 | 367 | // Implementing idempotent routing 368 | // by overriding page.js`s "show" method. 369 | // Why? 370 | // It is impossible to bypass exit triggers, 371 | // because they execute before the handler and 372 | // can not know what the next path is, inside exit trigger. 373 | // 374 | // we need override both show, replace to make this work 375 | // since we use redirect when we are talking about withReplaceState 376 | _.each(['show', 'replace'], function(fnName) { 377 | var original = self._page[fnName]; 378 | self._page[fnName] = function(path, state, dispatch, push) { 379 | var reload = self.env.reload.get(); 380 | if (!reload && self._current.path === path) { 381 | return; 382 | } 383 | 384 | original.call(this, path, state, dispatch, push); 385 | }; 386 | }); 387 | 388 | // this is very ugly part of pagejs and it does decoding few times 389 | // in unpredicatable manner. See #168 390 | // this is the default behaviour and we need keep it like that 391 | // we are doing a hack. see .path() 392 | this._page.base(this._basePath); 393 | this._page({ 394 | decodeURLComponents: true, 395 | hashbang: !!options.hashbang 396 | }); 397 | 398 | this._initialized = true; 399 | }; 400 | 401 | Router.prototype._buildTracker = function() { 402 | var self = this; 403 | 404 | // main autorun function 405 | var tracker = Tracker.autorun(function () { 406 | if(!self._current || !self._current.route) { 407 | return; 408 | } 409 | 410 | // see the definition of `this._processingContexts` 411 | var currentContext = self._current; 412 | var route = currentContext.route; 413 | var path = currentContext.path; 414 | 415 | if(self.safeToRun === 0) { 416 | var message = 417 | "You can't use reactive data sources like Session" + 418 | " inside the `.subscriptions` method!"; 419 | throw new Error(message); 420 | } 421 | 422 | // We need to run subscriptions inside a Tracker 423 | // to stop subs when switching between routes 424 | // But we don't need to run this tracker with 425 | // other reactive changes inside the .subscription method 426 | // We tackle this with the `safeToRun` variable 427 | self._globalRoute.clearSubscriptions(); 428 | self.subscriptions.call(self._globalRoute, path); 429 | route.callSubscriptions(currentContext); 430 | 431 | // otherwise, computations inside action will trigger to re-run 432 | // this computation. which we do not need. 433 | Tracker.nonreactive(function() { 434 | var isRouteChange = currentContext.oldRoute !== currentContext.route; 435 | var isFirstRoute = !currentContext.oldRoute; 436 | // first route is not a route change 437 | if(isFirstRoute) { 438 | isRouteChange = false; 439 | } 440 | 441 | // Clear oldRouteChain just before calling the action 442 | // We still need to get a copy of the oldestRoute first 443 | // It's very important to get the oldest route and registerRouteClose() it 444 | // See: https://github.com/kadirahq/flow-router/issues/314 445 | var oldestRoute = self._oldRouteChain[0]; 446 | self._oldRouteChain = []; 447 | 448 | currentContext.route.registerRouteChange(currentContext, isRouteChange); 449 | route.callAction(currentContext); 450 | 451 | Tracker.afterFlush(function() { 452 | self._onEveryPath.changed(); 453 | if(isRouteChange) { 454 | // We need to trigger that route (definition itself) has changed. 455 | // So, we need to re-run all the register callbacks to current route 456 | // This is pretty important, otherwise tracker 457 | // can't identify new route's items 458 | 459 | // We also need to afterFlush, otherwise this will re-run 460 | // helpers on templates which are marked for destroying 461 | if(oldestRoute) { 462 | oldestRoute.registerRouteClose(); 463 | } 464 | } 465 | }); 466 | }); 467 | 468 | self.safeToRun--; 469 | }); 470 | 471 | return tracker; 472 | }; 473 | 474 | Router.prototype._invalidateTracker = function() { 475 | var self = this; 476 | this.safeToRun++; 477 | this._tracker.invalidate(); 478 | // After the invalidation we need to flush to make changes imediately 479 | // otherwise, we have face some issues context mix-maches and so on. 480 | // But there are some cases we can't flush. So we need to ready for that. 481 | 482 | // we clearly know, we can't flush inside an autorun 483 | // this may leads some issues on flow-routing 484 | // we may need to do some warning 485 | if(!Tracker.currentComputation) { 486 | // Still there are some cases where we can't flush 487 | // eg:- when there is a flush currently 488 | // But we've no public API or hacks to get that state 489 | // So, this is the only solution 490 | try { 491 | Tracker.flush(); 492 | } catch(ex) { 493 | // only handling "while flushing" errors 494 | if(!/Tracker\.flush while flushing/.test(ex.message)) { 495 | return; 496 | } 497 | 498 | // XXX: fix this with a proper solution by removing subscription mgt. 499 | // from the router. Then we don't need to run invalidate using a tracker 500 | 501 | // this happens when we are trying to invoke a route change 502 | // with inside a route chnage. (eg:- Template.onCreated) 503 | // Since we use page.js and tracker, we don't have much control 504 | // over this process. 505 | // only solution is to defer route execution. 506 | 507 | // It's possible to have more than one path want to defer 508 | // But, we only need to pick the last one. 509 | // self._nextPath = self._current.path; 510 | Meteor.defer(function() { 511 | var path = self._nextPath; 512 | if(!path) { 513 | return; 514 | } 515 | 516 | delete self._nextPath; 517 | self.env.reload.withValue(true, function() { 518 | self.go(path); 519 | }); 520 | }); 521 | } 522 | } 523 | }; 524 | 525 | Router.prototype._updateCallbacks = function () { 526 | var self = this; 527 | 528 | self._page.callbacks = []; 529 | self._page.exits = []; 530 | 531 | _.each(self._routes, function(route) { 532 | self._page(route.pathDef, route._actionHandle); 533 | self._page.exit(route.pathDef, route._exitHandle); 534 | }); 535 | 536 | self._page("*", function(context) { 537 | self._notfoundRoute(context); 538 | }); 539 | }; 540 | 541 | Router.prototype._initTriggersAPI = function() { 542 | var self = this; 543 | this.triggers = { 544 | enter: function(triggers, filter) { 545 | triggers = Triggers.applyFilters(triggers, filter); 546 | if(triggers.length) { 547 | self._triggersEnter = self._triggersEnter.concat(triggers); 548 | } 549 | }, 550 | 551 | exit: function(triggers, filter) { 552 | triggers = Triggers.applyFilters(triggers, filter); 553 | if(triggers.length) { 554 | self._triggersExit = self._triggersExit.concat(triggers); 555 | } 556 | } 557 | }; 558 | }; 559 | 560 | Router.prototype.wait = function() { 561 | if(this._initialized) { 562 | throw new Error("can't wait after FineRouter has been initialized"); 563 | } 564 | 565 | this._askedToWait = true; 566 | }; 567 | 568 | Router.prototype.onRouteRegister = function(cb) { 569 | this._onRouteCallbacks.push(cb); 570 | }; 571 | 572 | Router.prototype._triggerRouteRegister = function(currentRoute) { 573 | // We should only need to send a safe set of fields on the route 574 | // object. 575 | // This is not to hide what's inside the route object, but to show 576 | // these are the public APIs 577 | var routePublicApi = _.pick(currentRoute, 'name', 'pathDef', 'path'); 578 | var omittingOptionFields = [ 579 | 'triggersEnter', 'triggersExit', 'action', 'subscriptions', 'name' 580 | ]; 581 | routePublicApi.options = _.omit(currentRoute.options, omittingOptionFields); 582 | 583 | _.each(this._onRouteCallbacks, function(cb) { 584 | cb(routePublicApi); 585 | }); 586 | }; 587 | 588 | Router.prototype._page = page; 589 | Router.prototype._qs = qs; 590 | -------------------------------------------------------------------------------- /client/triggers.js: -------------------------------------------------------------------------------- 1 | // a set of utility functions for triggers 2 | 3 | Triggers = {}; 4 | 5 | // Apply filters for a set of triggers 6 | // @triggers - a set of triggers 7 | // @filter - filter with array fileds with `only` and `except` 8 | // support only either `only` or `except`, but not both 9 | Triggers.applyFilters = function(triggers, filter) { 10 | if(!(triggers instanceof Array)) { 11 | triggers = [triggers]; 12 | } 13 | 14 | if(!filter) { 15 | return triggers; 16 | } 17 | 18 | if(filter.only && filter.except) { 19 | throw new Error("Triggers don't support only and except filters at once"); 20 | } 21 | 22 | if(filter.only && !(filter.only instanceof Array)) { 23 | throw new Error("only filters needs to be an array"); 24 | } 25 | 26 | if(filter.except && !(filter.except instanceof Array)) { 27 | throw new Error("except filters needs to be an array"); 28 | } 29 | 30 | if(filter.only) { 31 | return Triggers.createRouteBoundTriggers(triggers, filter.only); 32 | } 33 | 34 | if(filter.except) { 35 | return Triggers.createRouteBoundTriggers(triggers, filter.except, true); 36 | } 37 | 38 | throw new Error("Provided a filter but not supported"); 39 | }; 40 | 41 | // create triggers by bounding them to a set of route names 42 | // @triggers - a set of triggers 43 | // @names - list of route names to be bound (trigger runs only for these names) 44 | // @negate - negate the result (triggers won't run for above names) 45 | Triggers.createRouteBoundTriggers = function(triggers, names, negate) { 46 | var namesMap = {}; 47 | _.each(names, function(name) { 48 | namesMap[name] = true; 49 | }); 50 | 51 | var filteredTriggers = _.map(triggers, function(originalTrigger) { 52 | var modifiedTrigger = function(context, next) { 53 | var routeName = context.route.name; 54 | var matched = (namesMap[routeName])? 1: -1; 55 | matched = (negate)? matched * -1 : matched; 56 | 57 | if(matched === 1) { 58 | originalTrigger(context, next); 59 | } 60 | }; 61 | return modifiedTrigger; 62 | }); 63 | 64 | return filteredTriggers; 65 | }; 66 | 67 | // run triggers and abort if redirected or callback stopped 68 | // @triggers - a set of triggers 69 | // @context - context we need to pass (it must have the route) 70 | // @redirectFn - function which used to redirect 71 | // @after - called after if only all the triggers runs 72 | Triggers.runTriggers = function(triggers, context, redirectFn, after) { 73 | var abort = false; 74 | var inCurrentLoop = true; 75 | var alreadyRedirected = false; 76 | 77 | for(var lc=0; lc 0)? firstRegexpChar: undefined); 45 | // remove +?* 46 | key = key.replace(/[\+\*\?]+/g, ""); 47 | 48 | return fields[key] || ""; 49 | }); 50 | 51 | path = path.replace(/\/\/+/g, "/"); // Replace multiple slashes with single slash 52 | 53 | // remove trailing slash 54 | // but keep the root slash if it's the only one 55 | path = path.match(/^\/{1}$/) ? path: path.replace(/\/$/, ""); 56 | 57 | var strQueryParams = Qs.stringify(queryParams || {}); 58 | if(strQueryParams) { 59 | path += "?" + strQueryParams; 60 | } 61 | 62 | return path; 63 | }; 64 | 65 | Router.prototype.onRouteRegister = function(cb) { 66 | this._onRouteCallbacks.push(cb); 67 | }; 68 | 69 | Router.prototype._triggerRouteRegister = function(currentRoute) { 70 | // We should only need to send a safe set of fields on the route 71 | // object. 72 | // This is not to hide what's inside the route object, but to show 73 | // these are the public APIs 74 | var routePublicApi = _.pick(currentRoute, 'name', 'pathDef', 'path'); 75 | var omittingOptionFields = [ 76 | 'triggersEnter', 'triggersExit', 'action', 'subscriptions', 'name' 77 | ]; 78 | routePublicApi.options = _.omit(currentRoute.options, omittingOptionFields); 79 | 80 | _.each(this._onRouteCallbacks, function(cb) { 81 | cb(routePublicApi); 82 | }); 83 | }; 84 | 85 | 86 | Router.prototype.go = function() { 87 | // client only 88 | }; 89 | 90 | 91 | Router.prototype.current = function() { 92 | // client only 93 | }; 94 | 95 | 96 | Router.prototype.triggers = { 97 | enter: function() { 98 | // client only 99 | }, 100 | exit: function() { 101 | // client only 102 | } 103 | }; 104 | 105 | Router.prototype.middleware = function() { 106 | // client only 107 | }; 108 | 109 | 110 | Router.prototype.getState = function() { 111 | // client only 112 | }; 113 | 114 | 115 | Router.prototype.getAllStates = function() { 116 | // client only 117 | }; 118 | 119 | 120 | Router.prototype.setState = function() { 121 | // client only 122 | }; 123 | 124 | 125 | Router.prototype.removeState = function() { 126 | // client only 127 | }; 128 | 129 | 130 | Router.prototype.clearStates = function() { 131 | // client only 132 | }; 133 | 134 | 135 | Router.prototype.ready = function() { 136 | // client only 137 | }; 138 | 139 | 140 | Router.prototype.initialize = function() { 141 | // client only 142 | }; 143 | 144 | Router.prototype.wait = function() { 145 | // client only 146 | }; 147 | -------------------------------------------------------------------------------- /test/client/_helpers.js: -------------------------------------------------------------------------------- 1 | GetSub = function (name) { 2 | for(var id in Meteor.connection._subscriptions) { 3 | var sub = Meteor.connection._subscriptions[id]; 4 | if(name === sub.name) { 5 | return sub; 6 | } 7 | } 8 | }; 9 | 10 | FineRouter.route('/'); 11 | -------------------------------------------------------------------------------- /test/client/group.spec.js: -------------------------------------------------------------------------------- 1 | Tinytest.add('Client - Group - validate path definition', function (test, next) { 2 | // path & prefix must start with '/' 3 | test.throws(function() { 4 | new Group(null, {prefix: Random.id()}); 5 | }); 6 | 7 | var group = FineRouter.group({prefix: '/' + Random.id()}); 8 | 9 | test.throws(function() { 10 | group.route(Random.id()); 11 | }); 12 | }); 13 | 14 | Tinytest.addAsync('Client - Group - define and go to route with prefix', function (test, next) { 15 | var prefix = Random.id(); 16 | var rand = Random.id(); 17 | var rendered = 0; 18 | 19 | var group = FineRouter.group({prefix: '/' + prefix}); 20 | 21 | group.route('/' + rand, { 22 | action: function(_params) { 23 | rendered++; 24 | } 25 | }); 26 | 27 | FineRouter.go('/' + prefix + '/' + rand); 28 | 29 | setTimeout(function() { 30 | test.equal(rendered, 1); 31 | setTimeout(next, 100); 32 | }, 100); 33 | }); 34 | 35 | Tinytest.addAsync('Client - Group - define and go to route without prefix', function (test, next) { 36 | var rand = Random.id(); 37 | var rendered = 0; 38 | 39 | var group = FineRouter.group(); 40 | 41 | group.route('/' + rand, { 42 | action: function(_params) { 43 | rendered++; 44 | } 45 | }); 46 | 47 | FineRouter.go('/' + rand); 48 | 49 | setTimeout(function() { 50 | test.equal(rendered, 1); 51 | setTimeout(next, 100); 52 | }, 100); 53 | }); 54 | 55 | Tinytest.addAsync('Client - Group - subscribe', function (test, next) { 56 | var rand = Random.id(); 57 | 58 | var group = FineRouter.group({ 59 | subscriptions: function (params) { 60 | this.register('baz', Meteor.subscribe('baz')); 61 | } 62 | }); 63 | 64 | group.route('/' + rand); 65 | 66 | FineRouter.go('/' + rand); 67 | setTimeout(function() { 68 | test.isTrue(!!GetSub('baz')); 69 | next(); 70 | }, 100); 71 | }); 72 | 73 | 74 | Tinytest.addAsync('Client - Group - set and retrieve group name', function (test, next) { 75 | var rand = Random.id(); 76 | var name = Random.id(); 77 | 78 | var group = FineRouter.group({ 79 | name: name 80 | }); 81 | 82 | group.route('/' + rand); 83 | 84 | FineRouter.go('/' + rand); 85 | setTimeout(function() { 86 | test.isTrue(FineRouter.current().route.group.name === name); 87 | next(); 88 | }, 100); 89 | }); 90 | 91 | Tinytest.add('Client - Group - expose group options on a route', function (test) { 92 | var pathDef = "/" + Random.id(); 93 | var name = Random.id(); 94 | var groupName = Random.id(); 95 | var data = {aa: 10}; 96 | var layout = 'blah'; 97 | 98 | var group = FineRouter.group({ 99 | name: groupName, 100 | prefix: '/admin', 101 | layout: layout, 102 | someData: data 103 | }); 104 | 105 | group.route(pathDef, { 106 | name: name 107 | }); 108 | 109 | var route = FineRouter._routesMap[name]; 110 | 111 | test.equal(route.group.options.someData, data); 112 | test.equal(route.group.options.layout, layout); 113 | }); 114 | -------------------------------------------------------------------------------- /test/client/loader.spec.js: -------------------------------------------------------------------------------- 1 | Router = FineRouter.Router; 2 | 3 | 4 | Tinytest.add('Client - import page.js', function (test) { 5 | test.isTrue(!!Router.prototype._page); 6 | test.isFalse(!!window.page); 7 | }); 8 | 9 | 10 | Tinytest.add('Client - import query.js', function (test) { 11 | test.isTrue(!!Router.prototype._qs); 12 | }); 13 | 14 | 15 | Tinytest.add('Client - create FineRouter', function (test) { 16 | test.isTrue(!!FineRouter); 17 | }); 18 | -------------------------------------------------------------------------------- /test/client/route.reactivity.spec.js: -------------------------------------------------------------------------------- 1 | Route = FineRouter.Route; 2 | 3 | 4 | Tinytest.addAsync('Client - Route - Reactivity - getParam', function (test, done) { 5 | var r = new Route(); 6 | Tracker.autorun(function(c) { 7 | var param = r.getParam("id"); 8 | if(param) { 9 | test.equal(param, "hello"); 10 | c.stop(); 11 | Meteor.defer(done); 12 | } 13 | }); 14 | 15 | setTimeout(function() { 16 | var context = { 17 | params: {id: "hello"}, 18 | queryParams: {} 19 | }; 20 | r.registerRouteChange(context); 21 | }, 10); 22 | }); 23 | 24 | Tinytest.addAsync('Client - Route - Reactivity - getParam on route close', function (test, done) { 25 | var r = new Route(); 26 | var closeTriggered = false; 27 | Tracker.autorun(function(c) { 28 | var param = r.getParam("id"); 29 | if(closeTriggered) { 30 | test.equal(param, undefined); 31 | c.stop(); 32 | Meteor.defer(done); 33 | } 34 | }); 35 | 36 | setTimeout(function() { 37 | closeTriggered = true; 38 | r.registerRouteClose(); 39 | }, 10); 40 | }); 41 | 42 | Tinytest.addAsync('Client - Route - Reactivity - getQueryParam', function (test, done) { 43 | var r = new Route(); 44 | Tracker.autorun(function(c) { 45 | var param = r.getQueryParam("id"); 46 | if(param) { 47 | test.equal(param, "hello"); 48 | c.stop(); 49 | Meteor.defer(done); 50 | } 51 | }); 52 | 53 | setTimeout(function() { 54 | var context = { 55 | params: {}, 56 | queryParams: {id: "hello"} 57 | }; 58 | r.registerRouteChange(context); 59 | }, 10); 60 | }); 61 | 62 | Tinytest.addAsync('Client - Route - Reactivity - getQueryParam on route close', function (test, done) { 63 | var r = new Route(); 64 | var closeTriggered = false; 65 | Tracker.autorun(function(c) { 66 | var param = r.getQueryParam("id"); 67 | if(closeTriggered) { 68 | test.equal(param, undefined); 69 | c.stop(); 70 | Meteor.defer(done); 71 | } 72 | }); 73 | 74 | setTimeout(function() { 75 | closeTriggered = true; 76 | r.registerRouteClose(); 77 | }, 10); 78 | }); 79 | 80 | Tinytest.addAsync('Client - Route - Reactivity - getRouteName rerun when route closed', function (test, done) { 81 | var r = new Route(); 82 | r.name = "my-route"; 83 | var closeTriggered = false; 84 | 85 | Tracker.autorun(function(c) { 86 | var name = r.getRouteName(); 87 | test.equal(name, r.name); 88 | 89 | if(closeTriggered) { 90 | c.stop(); 91 | Meteor.defer(done); 92 | } 93 | }); 94 | 95 | setTimeout(function() { 96 | closeTriggered = true; 97 | r.registerRouteClose(); 98 | }, 10); 99 | }); 100 | 101 | Tinytest.addAsync('Client - Route - Reactivity - watchPathChange when routeChange', function (test, done) { 102 | var r = new Route(); 103 | var pathChangeCounts = 0; 104 | 105 | var c = Tracker.autorun(function() { 106 | r.watchPathChange(); 107 | pathChangeCounts++; 108 | }); 109 | 110 | var context = { 111 | params: {}, 112 | queryParams: {} 113 | }; 114 | 115 | setTimeout(function() { 116 | r.registerRouteChange(context); 117 | setTimeout(checkAfterNormalRouteChange, 50); 118 | }, 10); 119 | 120 | function checkAfterNormalRouteChange() { 121 | test.equal(pathChangeCounts, 2); 122 | var lastRouteChange = true; 123 | r.registerRouteChange(context, lastRouteChange); 124 | setTimeout(checkAfterLastRouteChange, 10); 125 | } 126 | 127 | function checkAfterLastRouteChange() { 128 | test.equal(pathChangeCounts, 2); 129 | c.stop(); 130 | Meteor.defer(done); 131 | } 132 | }); 133 | 134 | Tinytest.addAsync('Client - Route - Reactivity - watchPathChange when routeClose', function (test, done) { 135 | var r = new Route(); 136 | var pathChangeCounts = 0; 137 | 138 | var c = Tracker.autorun(function() { 139 | r.watchPathChange(); 140 | pathChangeCounts++; 141 | }); 142 | 143 | var context = { 144 | params: {}, 145 | queryParams: {} 146 | }; 147 | 148 | setTimeout(function() { 149 | r.registerRouteClose(); 150 | setTimeout(checkAfterRouteClose, 10); 151 | }, 10); 152 | 153 | function checkAfterRouteClose() { 154 | test.equal(pathChangeCounts, 2); 155 | c.stop(); 156 | Meteor.defer(done); 157 | } 158 | }); -------------------------------------------------------------------------------- /test/client/router.core.spec.js: -------------------------------------------------------------------------------- 1 | Router = FineRouter.Router; 2 | 3 | Tinytest.addAsync('Client - Router - define and go to route', function (test, next) { 4 | var rand = Random.id(); 5 | var rendered = 0; 6 | 7 | FineRouter.route('/' + rand, { 8 | action: function(_params) { 9 | rendered++; 10 | } 11 | }); 12 | 13 | FineRouter.go('/' + rand); 14 | 15 | setTimeout(function() { 16 | test.equal(rendered, 1); 17 | setTimeout(next, 100); 18 | }, 100); 19 | }); 20 | 21 | Tinytest.addAsync('Client - Router - define and go to route with fields', 22 | function (test, next) { 23 | var rand = Random.id(); 24 | var pathDef = "/" + rand + "/:key"; 25 | var rendered = 0; 26 | 27 | FineRouter.route(pathDef, { 28 | action: function(params) { 29 | test.equal(params.key, "abc +@%"); 30 | rendered++; 31 | } 32 | }); 33 | 34 | FineRouter.go(pathDef, {key: "abc +@%"}); 35 | 36 | setTimeout(function() { 37 | test.equal(rendered, 1); 38 | setTimeout(next, 100); 39 | }, 100); 40 | }); 41 | 42 | Tinytest.addAsync('Client - Router - parse params and query', function (test, next) { 43 | var rand = Random.id(); 44 | var rendered = 0; 45 | var params = null; 46 | 47 | FineRouter.route('/' + rand + '/:foo', { 48 | action: function(_params) { 49 | rendered++; 50 | params = _params; 51 | } 52 | }); 53 | 54 | FineRouter.go('/' + rand + '/bar'); 55 | 56 | setTimeout(function() { 57 | test.equal(rendered, 1); 58 | test.equal(params.foo, 'bar'); 59 | setTimeout(next, 100); 60 | }, 100); 61 | }); 62 | 63 | Tinytest.addAsync('Client - Router - redirect using FineRouter.go', function (test, next) { 64 | var rand = Random.id(), rand2 = Random.id(); 65 | var log = []; 66 | var paths = ['/' + rand2, '/' + rand]; 67 | var done = false; 68 | 69 | FineRouter.route(paths[0], { 70 | action: function(_params) { 71 | log.push(1); 72 | FineRouter.go(paths[1]); 73 | } 74 | }); 75 | 76 | FineRouter.route(paths[1], { 77 | action: function(_params) { 78 | log.push(2); 79 | } 80 | }); 81 | 82 | FineRouter.go(paths[0]); 83 | 84 | setTimeout(function() { 85 | test.equal(log, [1, 2]); 86 | done = true; 87 | next(); 88 | }, 100); 89 | }); 90 | 91 | Tinytest.addAsync('Client - Router - get current route path', function (test, next) { 92 | var value = Random.id(); 93 | var randomValue = Random.id(); 94 | var pathDef = "/" + randomValue + '/:_id'; 95 | var path = "/" + randomValue + "/" + value; 96 | 97 | var detectedValue = null; 98 | 99 | FineRouter.route(pathDef, { 100 | action: function(params) { 101 | detectedValue = params._id; 102 | } 103 | }); 104 | 105 | FineRouter.go(path); 106 | 107 | Meteor.setTimeout(function() { 108 | test.equal(detectedValue, value); 109 | test.equal(FineRouter.current().path, path); 110 | next(); 111 | }, 50); 112 | }); 113 | 114 | Tinytest.addAsync('Client - Router - subscribe to global subs', function (test, next) { 115 | var rand = Random.id(); 116 | FineRouter.route('/' + rand); 117 | 118 | FineRouter.subscriptions = function (path) { 119 | test.equal(path, '/' + rand); 120 | this.register('baz', Meteor.subscribe('baz')); 121 | }; 122 | 123 | FineRouter.go('/' + rand); 124 | setTimeout(function() { 125 | test.isTrue(!!GetSub('baz')); 126 | FineRouter.subscriptions = Function.prototype; 127 | next(); 128 | }, 100); 129 | }); 130 | 131 | Tinytest.addAsync('Client - Router - setParams - generic', function (test, done) { 132 | var randomKey = Random.id(); 133 | var pathDef = "/" + randomKey + "/:cat/:id"; 134 | var paramsList = []; 135 | FineRouter.route(pathDef, { 136 | action: function(params) { 137 | paramsList.push(params); 138 | } 139 | }); 140 | 141 | FineRouter.go(pathDef, {cat: "meteor", id: "200"}); 142 | setTimeout(function() { 143 | // return done(); 144 | var success = FineRouter.setParams({id: "700"}); 145 | test.isTrue(success); 146 | setTimeout(validate, 50); 147 | }, 50); 148 | 149 | function validate() { 150 | test.equal(paramsList.length, 2); 151 | test.equal(_.pick(paramsList[0], "id", "cat"), {cat: "meteor", id: "200"}); 152 | test.equal(_.pick(paramsList[1], "id", "cat"), {cat: "meteor", id: "700"}); 153 | done(); 154 | } 155 | }); 156 | 157 | Tinytest.addAsync('Client - Router - setParams - preserve query strings', function (test, done) { 158 | var randomKey = Random.id(); 159 | var pathDef = "/" + randomKey + "/:cat/:id"; 160 | var paramsList = []; 161 | var queryParamsList = []; 162 | 163 | FineRouter.route(pathDef, { 164 | action: function(params, queryParams) { 165 | paramsList.push(params); 166 | queryParamsList.push(queryParams); 167 | } 168 | }); 169 | 170 | FineRouter.go(pathDef, {cat: "meteor", id: "200 +% / ad"}, {aa: "20 +%"}); 171 | setTimeout(function() { 172 | // return done(); 173 | var success = FineRouter.setParams({id: "700 +% / ad"}); 174 | test.isTrue(success); 175 | setTimeout(validate, 50); 176 | }, 50); 177 | 178 | function validate() { 179 | test.equal(paramsList.length, 2); 180 | test.equal(queryParamsList.length, 2); 181 | 182 | test.equal(_.pick(paramsList[0], "id", "cat"), {cat: "meteor", id: "200 +% / ad"}); 183 | test.equal(_.pick(paramsList[1], "id", "cat"), {cat: "meteor", id: "700 +% / ad"}); 184 | test.equal(queryParamsList, [{aa: "20 +%"}, {aa: "20 +%"}]); 185 | done(); 186 | } 187 | }); 188 | 189 | Tinytest.add('Client - Router - setParams - no route selected', function (test) { 190 | var originalRoute = FineRouter._current.route; 191 | FineRouter._current.route = undefined; 192 | var success = FineRouter.setParams({id: "800"}); 193 | test.isFalse(success); 194 | FineRouter._current.route = originalRoute; 195 | }); 196 | 197 | Tinytest.addAsync('Client - Router - setQueryParams - using check', function (test, done) { 198 | var randomKey = Random.id(); 199 | var pathDef = "/" + randomKey + ""; 200 | var queryParamsList = []; 201 | FineRouter.route(pathDef, { 202 | action: function(params, queryParams) { 203 | queryParamsList.push(queryParams); 204 | } 205 | }); 206 | 207 | FineRouter.go(pathDef, {}, {cat: "meteor", id: "200"}); 208 | setTimeout(function() { 209 | check(FineRouter.current().queryParams, {cat: String, id: String}); 210 | done(); 211 | }, 50); 212 | }); 213 | 214 | Tinytest.addAsync('Client - Router - setQueryParams - generic', function (test, done) { 215 | var randomKey = Random.id(); 216 | var pathDef = "/" + randomKey + ""; 217 | var queryParamsList = []; 218 | FineRouter.route(pathDef, { 219 | action: function(params, queryParams) { 220 | queryParamsList.push(queryParams); 221 | } 222 | }); 223 | 224 | FineRouter.go(pathDef, {}, {cat: "meteor", id: "200"}); 225 | setTimeout(function() { 226 | // return done(); 227 | var success = FineRouter.setQueryParams({id: "700"}); 228 | test.isTrue(success); 229 | setTimeout(validate, 50); 230 | }, 50); 231 | 232 | function validate() { 233 | test.equal(queryParamsList.length, 2); 234 | test.equal(_.pick(queryParamsList[0], "id", "cat"), {cat: "meteor", id: "200"}); 235 | test.equal(_.pick(queryParamsList[1], "id", "cat"), {cat: "meteor", id: "700"}); 236 | done(); 237 | } 238 | }); 239 | 240 | Tinytest.addAsync('Client - Router - setQueryParams - remove query param null', function (test, done) { 241 | var randomKey = Random.id(); 242 | var pathDef = "/" + randomKey + ""; 243 | var queryParamsList = []; 244 | FineRouter.route(pathDef, { 245 | action: function(params, queryParams) { 246 | queryParamsList.push(queryParams); 247 | } 248 | }); 249 | 250 | FineRouter.go(pathDef, {}, {cat: "meteor", id: "200"}); 251 | setTimeout(function() { 252 | var success = FineRouter.setQueryParams({id: "700", cat: null}); 253 | test.isTrue(success); 254 | setTimeout(validate, 50); 255 | }, 50); 256 | 257 | function validate() { 258 | test.equal(queryParamsList.length, 2); 259 | test.equal(_.pick(queryParamsList[0], "id", "cat"), {cat: "meteor", id: "200"}); 260 | test.equal(queryParamsList[1], {id: "700"}); 261 | done(); 262 | } 263 | }); 264 | 265 | Tinytest.addAsync('Client - Router - setQueryParams - remove query param undefined', function (test, done) { 266 | var randomKey = Random.id(); 267 | var pathDef = "/" + randomKey + ""; 268 | var queryParamsList = []; 269 | FineRouter.route(pathDef, { 270 | action: function(params, queryParams) { 271 | queryParamsList.push(queryParams); 272 | } 273 | }); 274 | 275 | FineRouter.go(pathDef, {}, {cat: "meteor", id: "200"}); 276 | setTimeout(function() { 277 | var success = FineRouter.setQueryParams({id: "700", cat: undefined}); 278 | test.isTrue(success); 279 | setTimeout(validate, 50); 280 | }, 50); 281 | 282 | function validate() { 283 | test.equal(queryParamsList.length, 2); 284 | test.equal(_.pick(queryParamsList[0], "id", "cat"), {cat: "meteor", id: "200"}); 285 | test.equal(queryParamsList[1], {id: "700"}); 286 | done(); 287 | } 288 | }); 289 | 290 | Tinytest.addAsync('Client - Router - setQueryParams - preserve params', function (test, done) { 291 | var randomKey = Random.id(); 292 | var pathDef = "/" + randomKey + "/:abc"; 293 | var queryParamsList = []; 294 | var paramsList = []; 295 | FineRouter.route(pathDef, { 296 | action: function(params, queryParams) { 297 | paramsList.push(params); 298 | queryParamsList.push(queryParams); 299 | } 300 | }); 301 | 302 | FineRouter.go(pathDef, {abc: "20"}, {cat: "meteor", id: "200"}); 303 | setTimeout(function() { 304 | // return done(); 305 | var success = FineRouter.setQueryParams({id: "700"}); 306 | test.isTrue(success); 307 | setTimeout(validate, 50); 308 | }, 50); 309 | 310 | function validate() { 311 | test.equal(queryParamsList.length, 2); 312 | test.equal(queryParamsList, [ 313 | {cat: "meteor", id: "200"}, {cat: "meteor", id: "700"} 314 | ]); 315 | 316 | test.equal(paramsList.length, 2); 317 | test.equal(_.pick(paramsList[0], "abc"), {abc: "20"}); 318 | test.equal(_.pick(paramsList[1], "abc"), {abc: "20"}); 319 | done(); 320 | } 321 | }); 322 | 323 | Tinytest.add('Client - Router - setQueryParams - no route selected', function (test) { 324 | var originalRoute = FineRouter._current.route; 325 | FineRouter._current.route = undefined; 326 | var success = FineRouter.setQueryParams({id: "800"}); 327 | test.isFalse(success); 328 | FineRouter._current.route = originalRoute; 329 | }); 330 | 331 | Tinytest.addAsync('Client - Router - notFound', function (test, done) { 332 | var data = []; 333 | FineRouter.notFound = { 334 | subscriptions: function() { 335 | data.push("subscriptions"); 336 | }, 337 | action: function() { 338 | data.push("action"); 339 | } 340 | }; 341 | 342 | FineRouter.go("/" + Random.id()); 343 | setTimeout(function() { 344 | test.equal(data, ["subscriptions", "action"]); 345 | done(); 346 | }, 50); 347 | }); 348 | 349 | Tinytest.addAsync('Client - Router - withReplaceState - enabled', 350 | function (test, done) { 351 | var pathDef = "/" + Random.id() + "/:id"; 352 | var originalRedirect = FineRouter._page.replace; 353 | var callCount = 0; 354 | FineRouter._page.replace = function(path) { 355 | callCount++; 356 | originalRedirect.call(FineRouter._page, path); 357 | }; 358 | 359 | FineRouter.route(pathDef, { 360 | name: name, 361 | action: function(params) { 362 | test.equal(params.id, "awesome"); 363 | test.equal(callCount, 1); 364 | FineRouter._page.replace = originalRedirect; 365 | // We don't use Meteor.defer here since it carries 366 | // Meteor.Environment vars too 367 | // Which breaks our test below 368 | setTimeout(done, 0); 369 | } 370 | }); 371 | 372 | FineRouter.withReplaceState(function() { 373 | FineRouter.go(pathDef, {id: "awesome"}); 374 | }); 375 | }); 376 | 377 | Tinytest.addAsync('Client - Router - withReplaceState - disabled', 378 | function (test, done) { 379 | var pathDef = "/" + Random.id() + "/:id"; 380 | var originalRedirect = FineRouter._page.replace; 381 | var callCount = 0; 382 | FineRouter._page.replace = function(path) { 383 | callCount++; 384 | originalRedirect.call(FineRouter._page, path); 385 | }; 386 | 387 | FineRouter.route(pathDef, { 388 | name: name, 389 | action: function(params) { 390 | test.equal(params.id, "awesome"); 391 | test.equal(callCount, 0); 392 | FineRouter._page.replace = originalRedirect; 393 | Meteor.defer(done); 394 | } 395 | }); 396 | 397 | FineRouter.go(pathDef, {id: "awesome"}); 398 | }); 399 | 400 | Tinytest.addAsync('Client - Router - withTrailingSlash - enabled', function (test, next) { 401 | var rand = Random.id(); 402 | var rendered = 0; 403 | 404 | FineRouter.route('/' + rand, { 405 | action: function(_params) { 406 | rendered++; 407 | } 408 | }); 409 | 410 | FineRouter.withTrailingSlash(function() { 411 | FineRouter.go('/' + rand); 412 | }); 413 | 414 | setTimeout(function() { 415 | test.equal(rendered, 1); 416 | test.equal(_.last(location.href), '/'); 417 | setTimeout(next, 100); 418 | }, 100); 419 | }); 420 | 421 | Tinytest.addAsync('Client - Router - idempotent routing - action', 422 | function (test, done) { 423 | var rand = Random.id(); 424 | var pathDef = "/" + rand; 425 | var rendered = 0; 426 | 427 | FineRouter.route(pathDef, { 428 | action: function(params) { 429 | rendered++; 430 | } 431 | }); 432 | 433 | FineRouter.go(pathDef); 434 | 435 | Meteor.defer(function() { 436 | FineRouter.go(pathDef); 437 | 438 | Meteor.defer(function() { 439 | test.equal(rendered, 1); 440 | done(); 441 | }); 442 | }); 443 | }); 444 | 445 | Tinytest.addAsync('Client - Router - idempotent routing - triggers', 446 | function (test, next) { 447 | var rand = Random.id(); 448 | var pathDef = "/" + rand; 449 | var runnedTriggers = 0; 450 | var done = false; 451 | 452 | var triggerFns = [function(params) { 453 | if (done) return; 454 | 455 | runnedTriggers++; 456 | }]; 457 | 458 | FineRouter.triggers.enter(triggerFns); 459 | 460 | FineRouter.route(pathDef, { 461 | triggersEnter: triggerFns, 462 | triggersExit: triggerFns 463 | }); 464 | 465 | FineRouter.go(pathDef); 466 | 467 | FineRouter.triggers.exit(triggerFns); 468 | 469 | Meteor.defer(function() { 470 | FineRouter.go(pathDef); 471 | 472 | Meteor.defer(function() { 473 | test.equal(runnedTriggers, 2); 474 | done = true; 475 | next(); 476 | }); 477 | }); 478 | }); 479 | 480 | Tinytest.addAsync('Client - Router - reload - action', 481 | function (test, done) { 482 | var rand = Random.id(); 483 | var pathDef = "/" + rand; 484 | var rendered = 0; 485 | 486 | FineRouter.route(pathDef, { 487 | action: function(params) { 488 | rendered++; 489 | } 490 | }); 491 | 492 | FineRouter.go(pathDef); 493 | 494 | Meteor.defer(function() { 495 | FineRouter.reload(); 496 | 497 | Meteor.defer(function() { 498 | test.equal(rendered, 2); 499 | done(); 500 | }); 501 | }); 502 | }); 503 | 504 | Tinytest.addAsync('Client - Router - reload - triggers', 505 | function (test, next) { 506 | var rand = Random.id(); 507 | var pathDef = "/" + rand; 508 | var runnedTriggers = 0; 509 | var done = false; 510 | 511 | var triggerFns = [function(params) { 512 | if (done) return; 513 | 514 | runnedTriggers++; 515 | }]; 516 | 517 | FineRouter.triggers.enter(triggerFns); 518 | 519 | FineRouter.route(pathDef, { 520 | triggersEnter: triggerFns, 521 | triggersExit: triggerFns 522 | }); 523 | 524 | FineRouter.go(pathDef); 525 | 526 | FineRouter.triggers.exit(triggerFns); 527 | 528 | Meteor.defer(function() { 529 | FineRouter.reload(); 530 | 531 | Meteor.defer(function() { 532 | test.equal(runnedTriggers, 6); 533 | done = true; 534 | next(); 535 | }); 536 | }); 537 | }); 538 | 539 | Tinytest.addAsync( 540 | 'Client - Router - wait - before initialize', 541 | function(test, done) { 542 | FineRouter._initialized = false; 543 | FineRouter.wait(); 544 | test.equal(FineRouter._askedToWait, true); 545 | 546 | FineRouter._initialized = true; 547 | FineRouter._askedToWait = false; 548 | done(); 549 | }); 550 | 551 | Tinytest.addAsync( 552 | 'Client - Router - wait - after initialized', 553 | function(test, done) { 554 | try { 555 | FineRouter.wait(); 556 | } catch(ex) { 557 | test.isTrue(/can't wait/.test(ex.message)); 558 | done(); 559 | } 560 | }); 561 | 562 | Tinytest.addAsync( 563 | 'Client - Router - initialize - after initialized', 564 | function(test, done) { 565 | try { 566 | FineRouter.initialize(); 567 | } catch(ex) { 568 | test.isTrue(/already initialized/.test(ex.message)); 569 | done(); 570 | } 571 | }); 572 | 573 | Tinytest.addAsync( 574 | 'Client - Router - base path - url updated', 575 | function(test, done) { 576 | var simulatedBasePath = '/fine'; 577 | var rand = Random.id(); 578 | FineRouter.route('/' + rand, { action: function() {} }); 579 | 580 | setBasePath(simulatedBasePath); 581 | FineRouter.go('/' + rand); 582 | setTimeout(function() { 583 | test.equal(location.pathname, simulatedBasePath + '/' + rand); 584 | resetBasePath(); 585 | done(); 586 | }, 100); 587 | }); 588 | 589 | Tinytest.addAsync( 590 | 'Client - Router - base path - route action called', 591 | function(test, done) { 592 | var simulatedBasePath = '/fine'; 593 | var rand = Random.id(); 594 | FineRouter.route('/' + rand, { 595 | action: function() { 596 | resetBasePath(); 597 | done(); 598 | } 599 | }); 600 | 601 | setBasePath(simulatedBasePath); 602 | FineRouter.go('/' + rand); 603 | }); 604 | 605 | Tinytest.add( 606 | 'Client - Router - base path - path generation', 607 | function(test, done) { 608 | _.each(['/fine', '/fine/', 'fine/', 'fine'], function(simulatedBasePath) { 609 | var rand = Random.id(); 610 | setBasePath(simulatedBasePath); 611 | test.equal(FineRouter.path('/' + rand), '/fine/' + rand); 612 | }); 613 | resetBasePath(); 614 | }); 615 | 616 | 617 | function setBasePath(path) { 618 | FineRouter._initialized = false; 619 | FineRouter._basePath = path; 620 | FineRouter.initialize(); 621 | } 622 | 623 | var defaultBasePath = FineRouter._basePath; 624 | function resetBasePath() { 625 | setBasePath(defaultBasePath); 626 | } 627 | 628 | function bind(obj, method) { 629 | return function() { 630 | obj[method].apply(obj, arguments); 631 | }; 632 | } 633 | -------------------------------------------------------------------------------- /test/client/router.reactivity.spec.js: -------------------------------------------------------------------------------- 1 | Tinytest.addAsync( 2 | 'Client - Router - Reactivity - detectChange only once', 3 | function (test, done) { 4 | var route = "/" + Random.id(); 5 | var name = Random.id(); 6 | FineRouter.route(route, {name: name}); 7 | 8 | var ranCount = 0; 9 | var pickedId = null; 10 | var c = Tracker.autorun(function() { 11 | ranCount++; 12 | pickedId = FineRouter.getQueryParam("id"); 13 | if(pickedId) { 14 | test.equal(pickedId, "hello"); 15 | test.equal(ranCount, 2); 16 | c.stop(); 17 | Meteor.defer(done); 18 | } 19 | }); 20 | 21 | setTimeout(function() { 22 | FineRouter.go(name, {}, {id: "hello"}); 23 | }, 2); 24 | }); 25 | 26 | Tinytest.addAsync( 27 | 'Client - Router - Reactivity - detectChange in the action', 28 | function (test, done) { 29 | var route = "/" + Random.id(); 30 | var name = Random.id(); 31 | FineRouter.route(route, { 32 | name: name, 33 | action: function() { 34 | var id = FineRouter.getQueryParam("id"); 35 | test.equal(id, "hello"); 36 | Meteor.defer(done); 37 | } 38 | }); 39 | 40 | setTimeout(function() { 41 | FineRouter.go(name, {}, {id: "hello"}); 42 | }, 2); 43 | }); 44 | 45 | Tinytest.addAsync( 46 | 'Client - Router - Reactivity - detect prev routeChange after new action', 47 | function (test, done) { 48 | var route1 = "/" + Random.id(); 49 | var name1 = Random.id(); 50 | var pickedName1 = null; 51 | 52 | var route2 = "/" + Random.id(); 53 | var name2 = Random.id(); 54 | var pickedName2 = Random.id(); 55 | 56 | FineRouter.route(route1, { 57 | name: name1, 58 | action: function() { 59 | Tracker.autorun(function(c) { 60 | pickedName1 = FineRouter.getRouteName(); 61 | if(pickedName1 == name2) { 62 | test.equal(pickedName1, pickedName2); 63 | c.stop(); 64 | Meteor.defer(done); 65 | } 66 | }); 67 | } 68 | }); 69 | 70 | FineRouter.route(route2, { 71 | name: name2, 72 | action: function() { 73 | pickedName2 = FineRouter.getRouteName(); 74 | test.equal(pickedName1, name1); 75 | test.equal(pickedName2, name2); 76 | } 77 | }); 78 | 79 | FineRouter.go(name1); 80 | Meteor.setTimeout(function() { 81 | FineRouter.go(name2); 82 | }, 10); 83 | }); 84 | 85 | Tinytest.addAsync( 86 | 'Client - Router - Reactivity - defer watchPathChange until new route rendered', 87 | function(test, done) { 88 | var route1 = "/" + Random.id(); 89 | var name1 = Random.id(); 90 | var pickedName1 = null; 91 | 92 | var route2 = "/" + Random.id(); 93 | var name2 = Random.id(); 94 | var pickedName2 = Random.id(); 95 | 96 | FineRouter.route(route1, { 97 | name: name1, 98 | action: function() { 99 | Tracker.autorun(function(c) { 100 | FineRouter.watchPathChange(); 101 | pickedName1 = FineRouter.current().route.name; 102 | if(pickedName1 == name2) { 103 | test.equal(pickedName1, pickedName2); 104 | c.stop(); 105 | Meteor.defer(done); 106 | } 107 | }); 108 | } 109 | }); 110 | 111 | FineRouter.route(route2, { 112 | name: name2, 113 | action: function() { 114 | pickedName2 = FineRouter.current().route.name; 115 | test.equal(pickedName1, name1); 116 | test.equal(pickedName2, name2); 117 | } 118 | }); 119 | 120 | FineRouter.go(name1); 121 | Meteor.setTimeout(function() { 122 | FineRouter.go(name2); 123 | }, 10); 124 | }); 125 | 126 | Tinytest.addAsync( 127 | 'Client - Router - Reactivity - reactive changes and trigger redirects', 128 | function(test, done) { 129 | var name1 = Random.id(); 130 | var route1 = "/" + name1; 131 | FineRouter.route(route1, { 132 | name: name1 133 | }); 134 | 135 | var name2 = Random.id(); 136 | var route2 = "/" + name2; 137 | FineRouter.route(route2, { 138 | name: name2, 139 | triggersEnter: [function(context, redirect) { 140 | redirect(name3); 141 | }] 142 | }); 143 | 144 | 145 | var name3 = Random.id(); 146 | var route3 = "/" + name3; 147 | FineRouter.route(route3, { 148 | name: name3 149 | }); 150 | 151 | var routeNamesFired = []; 152 | FineRouter.go(name1); 153 | 154 | var c = null; 155 | setTimeout(function() { 156 | c = Tracker.autorun(function(c) { 157 | routeNamesFired.push(FineRouter.getRouteName()); 158 | }); 159 | FineRouter.go(name2); 160 | }, 50); 161 | 162 | setTimeout(function() { 163 | c.stop(); 164 | test.equal(routeNamesFired, [name1, name3]); 165 | Meteor.defer(done); 166 | }, 250); 167 | }); 168 | 169 | Tinytest.addAsync( 170 | 'Client - Router - Reactivity - watchPathChange for every route change', 171 | function(test, done) { 172 | var route1 = "/" + Random.id(); 173 | var name1 = Random.id(); 174 | var pickedName1 = null; 175 | 176 | var route2 = "/" + Random.id(); 177 | var name2 = Random.id(); 178 | var pickedName2 = Random.id(); 179 | 180 | FineRouter.route(route1, { 181 | name: name1 182 | }); 183 | 184 | FineRouter.route(route2, { 185 | name: name2 186 | }); 187 | 188 | var ids = []; 189 | var c = Tracker.autorun(function() { 190 | FineRouter.watchPathChange(); 191 | ids.push(FineRouter.current().queryParams['id']); 192 | }); 193 | 194 | FineRouter.go(name1, {}, {id: "one"}); 195 | Meteor.setTimeout(function() { 196 | FineRouter.go(name1, {}, {id: "two"}); 197 | }, 10); 198 | 199 | Meteor.setTimeout(function() { 200 | FineRouter.go(name2, {}, {id: "three"}); 201 | }, 20); 202 | 203 | Meteor.setTimeout(function() { 204 | test.equal(ids, [undefined, "one", "two", "three"]); 205 | c.stop(); 206 | done(); 207 | }, 40); 208 | }); -------------------------------------------------------------------------------- /test/client/router.subs_ready.spec.js: -------------------------------------------------------------------------------- 1 | Tinytest.addAsync('Client - Router - subsReady - with no args - all subscriptions ready', function (test, next) { 2 | var rand = Random.id(); 3 | FineRouter.route('/' + rand, { 4 | subscriptions: function(params) { 5 | this.register('bar', Meteor.subscribe('bar')); 6 | this.register('foo', Meteor.subscribe('foo')); 7 | } 8 | }); 9 | 10 | FineRouter.subscriptions = function () { 11 | this.register('baz', Meteor.subscribe('baz')); 12 | }; 13 | 14 | FineRouter.go('/' + rand); 15 | 16 | Tracker.autorun(function(c) { 17 | if(FineRouter.subsReady()) { 18 | FineRouter.subscriptions = Function.prototype; 19 | next(); 20 | c.stop(); 21 | } 22 | }); 23 | }); 24 | 25 | Tinytest.addAsync('Client - Router - subsReady - with no args - all subscriptions does not ready', function (test, next) { 26 | var rand = Random.id(); 27 | FineRouter.route('/' + rand, { 28 | subscriptions: function(params) { 29 | this.register('fooNotReady', Meteor.subscribe('fooNotReady')); 30 | } 31 | }); 32 | 33 | FineRouter.subscriptions = function () { 34 | this.register('bazNotReady', Meteor.subscribe('bazNotReady')); 35 | }; 36 | 37 | FineRouter.go('/' + rand); 38 | setTimeout(function() { 39 | test.isTrue(!FineRouter.subsReady()); 40 | FineRouter.subscriptions = Function.prototype; 41 | next(); 42 | }, 100); 43 | }); 44 | 45 | Tinytest.addAsync('Client - Router - subsReady - with no args - global subscriptions does not ready', function (test, next) { 46 | var rand = Random.id(); 47 | FineRouter.route('/' + rand, { 48 | subscriptions: function(params) { 49 | this.register('bar', Meteor.subscribe('bar')); 50 | this.register('foo', Meteor.subscribe('foo')); 51 | } 52 | }); 53 | 54 | FineRouter.subscriptions = function () { 55 | this.register('bazNotReady', Meteor.subscribe('bazNotReady')); 56 | }; 57 | 58 | FineRouter.go('/' + rand); 59 | setTimeout(function() { 60 | test.isTrue(!FineRouter.subsReady()); 61 | FineRouter.subscriptions = Function.prototype; 62 | next(); 63 | }, 100); 64 | }); 65 | 66 | Tinytest.addAsync('Client - Router - subsReady - with no args - current subscriptions does not ready', function (test, next) { 67 | var rand = Random.id(); 68 | FineRouter.route('/' + rand, { 69 | subscriptions: function(params) { 70 | this.register('bar', Meteor.subscribe('bar')); 71 | this.register('fooNotReady', Meteor.subscribe('fooNotReady')); 72 | } 73 | }); 74 | 75 | FineRouter.subscriptions = function () { 76 | this.register('baz', Meteor.subscribe('baz')); 77 | }; 78 | 79 | FineRouter.go('/' + rand); 80 | setTimeout(function() { 81 | test.isTrue(!FineRouter.subsReady()); 82 | FineRouter.subscriptions = Function.prototype; 83 | next(); 84 | }, 100); 85 | }); 86 | 87 | Tinytest.addAsync('Client - Router - subsReady - with args - all subscriptions ready', function (test, next) { 88 | var rand = Random.id(); 89 | FineRouter.route('/' + rand, { 90 | subscriptions: function(params) { 91 | this.register('bar', Meteor.subscribe('bar')); 92 | this.register('foo', Meteor.subscribe('foo')); 93 | } 94 | }); 95 | 96 | FineRouter.subscriptions = function () { 97 | this.register('baz', Meteor.subscribe('baz')); 98 | }; 99 | 100 | FineRouter.go('/' + rand); 101 | Tracker.autorun(function(c) { 102 | if(FineRouter.subsReady('foo', 'baz')) { 103 | FineRouter.subscriptions = Function.prototype; 104 | next(); 105 | c.stop(); 106 | } 107 | }); 108 | }); 109 | 110 | Tinytest.addAsync('Client - Router - subsReady - with args - all subscriptions does not ready', function (test, next) { 111 | var rand = Random.id(); 112 | FineRouter.route('/' + rand, { 113 | subscriptions: function(params) { 114 | this.register('fooNotReady', Meteor.subscribe('fooNotReady')); 115 | } 116 | }); 117 | 118 | FineRouter.subscriptions = function () { 119 | this.register('bazNotReady', Meteor.subscribe('bazNotReady')); 120 | }; 121 | 122 | FineRouter.go('/' + rand); 123 | setTimeout(function() { 124 | test.isTrue(!FineRouter.subsReady('fooNotReady', 'bazNotReady')); 125 | FineRouter.subscriptions = Function.prototype; 126 | next(); 127 | }, 100); 128 | }); 129 | 130 | Tinytest.addAsync('Client - Router - subsReady - with args - global subscriptions does not ready', function (test, next) { 131 | var rand = Random.id(); 132 | FineRouter.route('/' + rand, { 133 | subscriptions: function(params) { 134 | this.register('bar', Meteor.subscribe('bar')); 135 | this.register('foo', Meteor.subscribe('foo')); 136 | } 137 | }); 138 | 139 | FineRouter.subscriptions = function () { 140 | this.register('bazNotReady', Meteor.subscribe('bazNotReady')); 141 | }; 142 | 143 | FineRouter.go('/' + rand); 144 | setTimeout(function() { 145 | test.isTrue(!FineRouter.subsReady('foo', 'bazNotReady')); 146 | FineRouter.subscriptions = Function.prototype; 147 | next(); 148 | }, 100); 149 | }); 150 | 151 | Tinytest.addAsync('Client - Router - subsReady - with args - current subscriptions does not ready', function (test, next) { 152 | var rand = Random.id(); 153 | FineRouter.route('/' + rand, { 154 | subscriptions: function(params) { 155 | this.register('bar', Meteor.subscribe('bar')); 156 | this.register('fooNotReady', Meteor.subscribe('fooNotReady')); 157 | } 158 | }); 159 | 160 | FineRouter.subscriptions = function () { 161 | this.register('baz', Meteor.subscribe('baz')); 162 | }; 163 | 164 | FineRouter.go('/' + rand); 165 | setTimeout(function() { 166 | test.isTrue(!FineRouter.subsReady('fooNotReady', 'baz')); 167 | FineRouter.subscriptions = Function.prototype; 168 | next(); 169 | }, 100); 170 | }); 171 | 172 | Tinytest.addAsync('Client - Router - subsReady - with args - subscribe with wrong name', function (test, next) { 173 | var rand = Random.id(); 174 | FineRouter.route('/' + rand, { 175 | subscriptions: function(params) { 176 | this.register('bar', Meteor.subscribe('bar')); 177 | } 178 | }); 179 | 180 | FineRouter.subscriptions = function () { 181 | this.register('baz', Meteor.subscribe('baz')); 182 | }; 183 | 184 | FineRouter.go('/' + rand); 185 | setTimeout(function() { 186 | test.isTrue(!FineRouter.subsReady('baz', 'xxx', 'baz')); 187 | FineRouter.subscriptions = Function.prototype; 188 | next(); 189 | }, 100); 190 | }); 191 | 192 | Tinytest.addAsync('Client - Router - subsReady - with args - same route two different subs', function (test, next) { 193 | var rand = Random.id(); 194 | var count = 0; 195 | FineRouter.route('/' + rand, { 196 | subscriptions: function(params) { 197 | if(++count == 1) { 198 | this.register('not-exisitng', Meteor.subscribe('not-exisitng')); 199 | } 200 | } 201 | }); 202 | 203 | FineRouter.subscriptions = Function.prototype; 204 | FineRouter.go('/' + rand); 205 | setTimeout(function() { 206 | test.isFalse(FineRouter.subsReady()); 207 | FineRouter.go('/' + rand, {}, {param: "111"}); 208 | setTimeout(function() { 209 | test.isTrue(FineRouter.subsReady()); 210 | next(); 211 | }, 100) 212 | }, 100); 213 | }); 214 | 215 | Tinytest.addAsync('Client - Router - subsReady - no subscriptions - simple', function (test, next) { 216 | var rand = Random.id(); 217 | FineRouter.route('/' + rand, {}); 218 | FineRouter.subscriptions = Function.prototype; 219 | 220 | FineRouter.go('/' + rand); 221 | setTimeout(function() { 222 | test.isTrue(FineRouter.subsReady()); 223 | next(); 224 | }, 100); 225 | }); -------------------------------------------------------------------------------- /test/client/trigger.spec.js: -------------------------------------------------------------------------------- 1 | Tinytest.addAsync('Client - Triggers - global enter triggers', function(test, next) { 2 | var rand = Random.id(), rand2 = Random.id(); 3 | var log = []; 4 | var paths = ['/' + rand2, '/' + rand]; 5 | var done = false; 6 | 7 | FineRouter.route('/' + rand, { 8 | action: function(_params) { 9 | log.push(1); 10 | } 11 | }); 12 | 13 | FineRouter.route('/' + rand2, { 14 | action: function(_params) { 15 | log.push(2); 16 | } 17 | }); 18 | 19 | FineRouter.triggers.enter([function(context) { 20 | if(done) return; 21 | test.equal(context.path, paths.pop()); 22 | log.push(0); 23 | }]); 24 | 25 | FineRouter.go('/' + rand); 26 | 27 | setTimeout(function() { 28 | FineRouter.go('/' + rand2); 29 | 30 | setTimeout(function() { 31 | test.equal(log, [0, 1, 0, 2]); 32 | done = true; 33 | setTimeout(next, 100); 34 | }, 100); 35 | }, 100); 36 | }); 37 | 38 | Tinytest.addAsync('Client - Triggers - global enter triggers with "only"', function (test, next) { 39 | var rand = Random.id(), rand2 = Random.id(); 40 | var log = []; 41 | var done = false; 42 | 43 | FineRouter.route('/' + rand, { 44 | action: function(_params) { 45 | log.push(1); 46 | } 47 | }); 48 | 49 | FineRouter.route('/' + rand2, { 50 | name: 'foo', 51 | action: function(_params) { 52 | log.push(2); 53 | } 54 | }); 55 | 56 | FineRouter.triggers.enter([function(context) { 57 | if(done) return; 58 | test.equal(context.path, '/' + rand2); 59 | log.push(8); 60 | }], {only: ['foo']}); 61 | 62 | FineRouter.go('/' + rand); 63 | 64 | setTimeout(function() { 65 | FineRouter.go('/' + rand2); 66 | 67 | setTimeout(function() { 68 | test.equal(log, [1, 8, 2]); 69 | done = true; 70 | setTimeout(next, 100); 71 | }, 100); 72 | }, 100); 73 | }); 74 | 75 | Tinytest.addAsync('Client - Triggers - global enter triggers with "except"', function (test, next) { 76 | var rand = Random.id(), rand2 = Random.id(); 77 | var log = []; 78 | var done = false; 79 | 80 | FineRouter.route('/' + rand, { 81 | action: function(_params) { 82 | log.push(1); 83 | } 84 | }); 85 | 86 | FineRouter.route('/' + rand2, { 87 | name: 'foo', 88 | action: function(_params) { 89 | log.push(2); 90 | } 91 | }); 92 | 93 | FineRouter.triggers.enter([function(context) { 94 | if(done) return; 95 | test.equal(context.path, '/' + rand); 96 | log.push(8); 97 | }], {except: ['foo']}); 98 | 99 | FineRouter.go('/' + rand); 100 | 101 | setTimeout(function() { 102 | FineRouter.go('/' + rand2); 103 | 104 | setTimeout(function() { 105 | test.equal(log, [8, 1, 2]); 106 | done = true; 107 | setTimeout(next, 100); 108 | }, 100); 109 | }, 100); 110 | }); 111 | 112 | Tinytest.addAsync('Client - Triggers - global exit triggers', function (test, next) { 113 | var rand = Random.id(), rand2 = Random.id(); 114 | var log = []; 115 | var done =false; 116 | 117 | FineRouter.route('/' + rand, { 118 | action: function(_params) { 119 | log.push(1); 120 | } 121 | }); 122 | 123 | FineRouter.route('/' + rand2, { 124 | action: function(_params) { 125 | log.push(2); 126 | } 127 | }); 128 | 129 | FineRouter.go('/' + rand); 130 | 131 | FineRouter.triggers.exit([function(context) { 132 | if(done) return; 133 | test.equal(context.path, '/' + rand); 134 | log.push(0); 135 | }]); 136 | 137 | setTimeout(function() { 138 | FineRouter.go('/' + rand2); 139 | 140 | setTimeout(function() { 141 | test.equal(log, [1, 0, 2]); 142 | done = true; 143 | setTimeout(next, 100); 144 | }, 100); 145 | }, 100); 146 | }); 147 | 148 | Tinytest.addAsync('Client - Triggers - global exit triggers with "only"', function (test, next) { 149 | var rand = Random.id(), rand2 = Random.id(); 150 | var log = []; 151 | var done = false; 152 | 153 | FineRouter.route('/' + rand, { 154 | action: function(_params) { 155 | log.push(1); 156 | } 157 | }); 158 | 159 | FineRouter.route('/' + rand2, { 160 | name: 'foo', 161 | action: function(_params) { 162 | log.push(2); 163 | } 164 | }); 165 | 166 | FineRouter.triggers.exit([function(context) { 167 | if(done) return; 168 | test.equal(context.path, '/' + rand2); 169 | log.push(8); 170 | }], {only: ['foo']}); 171 | 172 | FineRouter.go('/' + rand); 173 | 174 | setTimeout(function() { 175 | FineRouter.go('/' + rand2); 176 | 177 | setTimeout(function() { 178 | FineRouter.go('/' + rand); 179 | 180 | setTimeout(function() { 181 | test.equal(log, [1, 2, 8, 1]); 182 | done = true; 183 | setTimeout(next, 100); 184 | }, 100); 185 | }, 100); 186 | }, 100); 187 | }); 188 | 189 | Tinytest.addAsync('Client - Triggers - global exit triggers with "except"', function (test, next) { 190 | var rand = Random.id(), rand2 = Random.id(); 191 | var log = []; 192 | var done = false; 193 | 194 | FineRouter.route('/' + rand, { 195 | action: function(_params) { 196 | log.push(1); 197 | } 198 | }); 199 | 200 | FineRouter.route('/' + rand2, { 201 | name: 'foo', 202 | action: function(_params) { 203 | log.push(2); 204 | } 205 | }); 206 | 207 | FineRouter.go('/' + rand); 208 | 209 | FineRouter.triggers.exit([function(context) { 210 | if(done) return; 211 | test.equal(context.path, '/' + rand); 212 | log.push(9); 213 | }], {except: ['foo']}); 214 | 215 | 216 | setTimeout(function() { 217 | FineRouter.go('/' + rand2); 218 | 219 | setTimeout(function() { 220 | FineRouter.go('/' + rand); 221 | 222 | setTimeout(function() { 223 | test.equal(log, [1, 9, 2, 1]); 224 | done = true; 225 | setTimeout(next, 100); 226 | }, 100); 227 | }, 100); 228 | }, 100); 229 | }); 230 | 231 | Tinytest.addAsync('Client - Triggers - route enter triggers', function (test, next) { 232 | var rand = Random.id(); 233 | var log = []; 234 | 235 | var triggerFn = function (context) { 236 | test.equal(context.path, '/' + rand); 237 | log.push(5); 238 | }; 239 | 240 | FineRouter.route('/' + rand, { 241 | triggersEnter: [triggerFn], 242 | action: function(_params) { 243 | log.push(1); 244 | } 245 | }); 246 | 247 | FineRouter.go('/' + rand); 248 | 249 | setTimeout(function() { 250 | test.equal(log, [5, 1]); 251 | setTimeout(next, 100); 252 | }, 100); 253 | }); 254 | 255 | Tinytest.addAsync('Client - Triggers - router exit triggers', function (test, next) { 256 | var rand = Random.id(); 257 | var log = []; 258 | 259 | var triggerFn = function (context) { 260 | test.equal(context.path, '/' + rand); 261 | log.push(6); 262 | }; 263 | 264 | FineRouter.route('/' + rand, { 265 | triggersExit: [triggerFn], 266 | action: function(_params) { 267 | log.push(1); 268 | } 269 | }); 270 | 271 | FineRouter.go('/' + rand); 272 | 273 | setTimeout(function() { 274 | FineRouter.go('/' + Random.id()); 275 | 276 | setTimeout(function() { 277 | test.equal(log, [1, 6]); 278 | setTimeout(next, 100); 279 | }, 100); 280 | }, 100); 281 | }); 282 | 283 | Tinytest.addAsync('Client - Triggers - group enter triggers', function (test, next) { 284 | var rand = Random.id(), rand2 = Random.id(); 285 | var log = []; 286 | var paths = ['/' + rand2, '/' + rand]; 287 | 288 | var triggerFn = function (context) { 289 | test.equal(context.path, paths.pop()); 290 | log.push(3); 291 | }; 292 | 293 | var group = FineRouter.group({ 294 | triggersEnter: [triggerFn] 295 | }); 296 | 297 | group.route('/' + rand, { 298 | action: function(_params) { 299 | log.push(1); 300 | } 301 | }); 302 | 303 | group.route('/' + rand2, { 304 | action: function(_params) { 305 | log.push(2); 306 | } 307 | }); 308 | 309 | FineRouter.go('/' + rand); 310 | 311 | setTimeout(function() { 312 | FineRouter.go('/' + rand2); 313 | 314 | setTimeout(function() { 315 | test.equal(log, [3, 1, 3, 2]); 316 | setTimeout(next, 100); 317 | }, 100); 318 | }, 100); 319 | }); 320 | 321 | Tinytest.addAsync('Client - Triggers - group exit triggers', function (test, next) { 322 | var rand = Random.id(), rand2 = Random.id(); 323 | var log = []; 324 | 325 | var triggerFn = function (context) { 326 | log.push(4); 327 | }; 328 | 329 | var group = FineRouter.group({ 330 | triggersExit: [triggerFn] 331 | }); 332 | 333 | group.route('/' + rand, { 334 | action: function(_params) { 335 | log.push(1); 336 | } 337 | }); 338 | 339 | group.route('/' + rand2, { 340 | action: function(_params) { 341 | log.push(2); 342 | } 343 | }); 344 | 345 | FineRouter.go('/' + rand); 346 | 347 | setTimeout(function() { 348 | FineRouter.go('/' + rand2); 349 | 350 | setTimeout(function() { 351 | test.equal(log, [1, 4, 2]); 352 | setTimeout(next, 100); 353 | }, 100); 354 | }, 100); 355 | }); 356 | 357 | Tinytest.addAsync('Client - Triggers - redirect from enter', function(test, next) { 358 | var rand = Random.id(), rand2 = Random.id(); 359 | var log = []; 360 | 361 | FineRouter.route('/' + rand, { 362 | triggersEnter: [function(context, redirect) { 363 | redirect("/" + rand2); 364 | }, function() { 365 | throw new Error("should not execute this trigger"); 366 | }], 367 | action: function(_params) { 368 | log.push(1); 369 | }, 370 | name: rand 371 | }); 372 | 373 | FineRouter.route('/' + rand2, { 374 | action: function(_params) { 375 | log.push(2); 376 | }, 377 | name: rand2 378 | }); 379 | 380 | FineRouter.go('/'); 381 | FineRouter.go('/' + rand); 382 | 383 | setTimeout(function() { 384 | test.equal(log, [2]); 385 | next(); 386 | }, 300); 387 | }); 388 | 389 | Tinytest.addAsync('Client - Triggers - redirect by routeName', function(test, next) { 390 | var rand = Random.id(), rand2 = Random.id(); 391 | var log = []; 392 | 393 | FineRouter.route('/' + rand, { 394 | name: rand, 395 | triggersEnter: [function(context, redirect) { 396 | redirect(rand2, null, {aa: "bb"}); 397 | }, function() { 398 | throw new Error("should not execute this trigger"); 399 | }], 400 | action: function(_params) { 401 | log.push(1); 402 | }, 403 | name: rand 404 | }); 405 | 406 | FineRouter.route('/' + rand2, { 407 | name: rand2, 408 | action: function(_params, queryParams) { 409 | log.push(2); 410 | test.equal(queryParams, {aa: "bb"}); 411 | }, 412 | name: rand2 413 | }); 414 | 415 | FineRouter.go('/'); 416 | FineRouter.go('/' + rand); 417 | 418 | setTimeout(function() { 419 | test.equal(log, [2]); 420 | next(); 421 | }, 300); 422 | }); 423 | 424 | Tinytest.addAsync('Client - Triggers - redirect from exit', function(test, next) { 425 | var rand = Random.id(), rand2 = Random.id(), rand3 = Random.id(); 426 | var log = []; 427 | 428 | FineRouter.route('/' + rand, { 429 | action: function() { 430 | log.push(1); 431 | }, 432 | triggersExit: [ 433 | function(context, redirect) { 434 | redirect('/' + rand3); 435 | }, 436 | function() { 437 | throw new Error("should not call this trigger"); 438 | } 439 | ] 440 | }); 441 | 442 | FineRouter.route('/' + rand2, { 443 | action: function() { 444 | log.push(2); 445 | } 446 | }); 447 | 448 | FineRouter.route('/' + rand3, { 449 | action: function() { 450 | log.push(3); 451 | } 452 | }); 453 | 454 | FineRouter.go('/' + rand); 455 | 456 | setTimeout(function() { 457 | FineRouter.go('/' + rand2); 458 | 459 | setTimeout(function() { 460 | test.equal(log, [1, 3]); 461 | next(); 462 | }, 100); 463 | }, 100); 464 | }); 465 | 466 | Tinytest.addAsync('Client - Triggers - redirect to external URL fails', function(test, next) { 467 | var rand = Random.id(), rand2 = Random.id(); 468 | var log = []; 469 | 470 | // testing "http://" URLs 471 | FineRouter.route('/' + rand, { 472 | triggersEnter: [function(context, redirect) { 473 | test.throws(function() { 474 | redirect("http://example.com/") 475 | }, "Redirects to URLs outside of the app are not supported") 476 | }], 477 | action: function(_params) { 478 | log.push(1); 479 | }, 480 | name: rand 481 | }); 482 | 483 | // testing "https://" URLs 484 | FineRouter.route('/' + rand2, { 485 | triggersEnter: [function(context, redirect) { 486 | test.throws(function() { 487 | redirect("https://example.com/") 488 | }) 489 | }], 490 | action: function(_params) { 491 | log.push(2); 492 | }, 493 | name: rand2 494 | }); 495 | 496 | FineRouter.go('/'); 497 | FineRouter.go('/' + rand); 498 | FineRouter.go('/' + rand2); 499 | 500 | setTimeout(function() { 501 | test.equal(log, []); 502 | next(); 503 | }, 300); 504 | }); 505 | 506 | Tinytest.addAsync('Client - Triggers - stop callback from enter', function(test, next) { 507 | var rand = Random.id(); 508 | var log = []; 509 | 510 | FineRouter.route('/' + rand, { 511 | triggersEnter: [function(context, redirect, stop) { 512 | log.push(10); 513 | stop(); 514 | }, function() { 515 | throw new Error("should not execute this trigger"); 516 | }], 517 | action: function(_params) { 518 | throw new Error("should not execute the action"); 519 | } 520 | }); 521 | 522 | FineRouter.go('/'); 523 | FineRouter.go('/' + rand); 524 | 525 | setTimeout(function() { 526 | test.equal(log, [10]); 527 | next(); 528 | }, 100); 529 | }); 530 | 531 | Tinytest.addAsync( 532 | 'Client - Triggers - invalidate inside an autorun', 533 | function(test, next) { 534 | var rand = Random.id(), rand2 = Random.id(); 535 | var log = []; 536 | var paths = ['/' + rand2, '/' + rand]; 537 | var done = false; 538 | 539 | FineRouter.route('/' + rand, { 540 | action: function(_params) { 541 | log.push(1); 542 | } 543 | }); 544 | 545 | FineRouter.route('/' + rand2, { 546 | action: function(_params) { 547 | log.push(2); 548 | } 549 | }); 550 | 551 | FineRouter.triggers.enter([function(context) { 552 | if(done) return; 553 | test.equal(context.path, paths.pop()); 554 | log.push(0); 555 | }]); 556 | 557 | Tracker.autorun(function(c) { 558 | FineRouter.go('/' + rand); 559 | }); 560 | 561 | setTimeout(function() { 562 | FineRouter.go('/' + rand2); 563 | 564 | setTimeout(function() { 565 | test.equal(log, [0, 1, 0, 2]); 566 | done = true; 567 | setTimeout(next, 100); 568 | }, 100); 569 | }, 100); 570 | }); 571 | -------------------------------------------------------------------------------- /test/client/triggers.js: -------------------------------------------------------------------------------- 1 | Tinytest.addAsync( 2 | 'Triggers - runTriggers - run all and after', 3 | function(test, done) { 4 | var store = []; 5 | var triggers = MakeTriggers(2, store); 6 | Triggers.runTriggers(triggers, null, null, function() { 7 | test.equal(store, [0, 1]); 8 | done(); 9 | }); 10 | }); 11 | 12 | Tinytest.addAsync( 13 | 'Triggers - runTriggers - redirect with url', 14 | function(test, done) { 15 | var store = []; 16 | var url = "http://google.com"; 17 | var triggers = MakeTriggers(2, store); 18 | triggers.splice(1, 0, function(context, redirect) { 19 | redirect(url); 20 | }); 21 | 22 | Triggers.runTriggers(triggers, null, function(u) { 23 | test.equal(store, [0]); 24 | test.equal(u, url); 25 | done(); 26 | }, null); 27 | }); 28 | 29 | Tinytest.addAsync( 30 | 'Triggers - runTriggers - redirect without url', 31 | function(test, done) { 32 | var store = []; 33 | var url = "http://google.com"; 34 | var triggers = MakeTriggers(2, store); 35 | triggers.splice(1, 0, function(context, redirect) { 36 | try { 37 | redirect(); 38 | } catch(ex) { 39 | test.isTrue(/requires an URL/.test(ex.message)); 40 | test.equal(store, [0]); 41 | done(); 42 | } 43 | }); 44 | 45 | Triggers.runTriggers(triggers, null, null, null); 46 | }); 47 | 48 | Tinytest.addAsync( 49 | 'Triggers - runTriggers - redirect in a different event loop', 50 | function(test, done) { 51 | var store = []; 52 | var url = "http://google.com"; 53 | var triggers = MakeTriggers(2, store); 54 | var doneCalled = false; 55 | 56 | triggers.splice(1, 0, function(context, redirect) { 57 | setTimeout(function() { 58 | try { 59 | redirect(url); 60 | } catch(ex) { 61 | test.isTrue(/sync/.test(ex.message)); 62 | test.equal(store, [0, 1]); 63 | test.isTrue(doneCalled); 64 | done(); 65 | } 66 | }, 0); 67 | }); 68 | 69 | Triggers.runTriggers(triggers, null, null, function() { 70 | doneCalled = true; 71 | }); 72 | }); 73 | 74 | Tinytest.addAsync( 75 | 'Triggers - runTriggers - redirect called multiple times', 76 | function(test, done) { 77 | var store = []; 78 | var url = "http://google.com"; 79 | var triggers = MakeTriggers(2, store); 80 | var redirectCalled = false; 81 | 82 | triggers.splice(1, 0, function(context, redirect) { 83 | redirect(url); 84 | try { 85 | redirect(url); 86 | } catch(ex) { 87 | test.isTrue(/already redirected/.test(ex.message)); 88 | test.equal(store, [0]); 89 | test.isTrue(redirectCalled); 90 | done(); 91 | } 92 | }); 93 | 94 | Triggers.runTriggers(triggers, null, function() { 95 | redirectCalled = true; 96 | }, null); 97 | }); 98 | 99 | Tinytest.addAsync( 100 | 'Triggers - runTriggers - stop callback', 101 | function(test, done) { 102 | var store = []; 103 | var triggers = MakeTriggers(2, store); 104 | triggers.splice(1, 0, function(context, redirect, stop) { 105 | stop(); 106 | }); 107 | 108 | Triggers.runTriggers(triggers, null, null, function() { 109 | store.push(2); 110 | }); 111 | 112 | test.equal(store, [0]); 113 | done(); 114 | }); 115 | 116 | 117 | Tinytest.addAsync( 118 | 'Triggers - runTriggers - get context', 119 | function(test, done) { 120 | var context = {}; 121 | var trigger = function(c) { 122 | test.equal(c, context); 123 | done(); 124 | }; 125 | 126 | Triggers.runTriggers([trigger], context, function() {}, function() {}); 127 | }); 128 | 129 | Tinytest.addAsync( 130 | 'Triggers - createRouteBoundTriggers - matching trigger', 131 | function(test, done) { 132 | var context = {route: {name: "abc"}}; 133 | var redirect = function() {}; 134 | 135 | var trigger = function(c, r) { 136 | test.equal(c, context); 137 | test.equal(r, redirect); 138 | done(); 139 | }; 140 | 141 | var triggers = Triggers.createRouteBoundTriggers([trigger], ["abc"]); 142 | triggers[0](context, redirect); 143 | }); 144 | 145 | Tinytest.addAsync( 146 | 'Triggers - createRouteBoundTriggers - multiple matching triggers', 147 | function(test, done) { 148 | var context = {route: {name: "abc"}}; 149 | var redirect = function() {}; 150 | var doneCount = 0; 151 | 152 | var trigger = function(c, r) { 153 | test.equal(c, context); 154 | test.equal(r, redirect); 155 | doneCount++; 156 | }; 157 | 158 | var triggers = Triggers.createRouteBoundTriggers([trigger, trigger], ["abc"]); 159 | triggers[0](context, redirect); 160 | triggers[1](context, redirect); 161 | 162 | test.equal(doneCount, 2); 163 | done(); 164 | }); 165 | 166 | Tinytest.addAsync( 167 | 'Triggers - createRouteBoundTriggers - no matching trigger', 168 | function(test, done) { 169 | var context = {route: {name: "some-other-route"}}; 170 | var redirect = function() {}; 171 | var doneCount = 0; 172 | 173 | var trigger = function(c, r) { 174 | test.equal(c, context); 175 | test.equal(r, redirect); 176 | doneCount++; 177 | }; 178 | 179 | var triggers = Triggers.createRouteBoundTriggers([trigger], ["abc"]); 180 | triggers[0](context, redirect); 181 | 182 | test.equal(doneCount, 0); 183 | done(); 184 | }); 185 | 186 | Tinytest.addAsync( 187 | 'Triggers - createRouteBoundTriggers - negate logic', 188 | function(test, done) { 189 | var context = {route: {name: "some-other-route"}}; 190 | var redirect = function() {}; 191 | var doneCount = 0; 192 | 193 | var trigger = function(c, r) { 194 | test.equal(c, context); 195 | test.equal(r, redirect); 196 | doneCount++; 197 | }; 198 | 199 | var triggers = Triggers.createRouteBoundTriggers([trigger], ["abc"], true); 200 | triggers[0](context, redirect); 201 | 202 | test.equal(doneCount, 1); 203 | done(); 204 | }); 205 | 206 | Tinytest.addAsync( 207 | 'Triggers - applyFilters - no filters', 208 | function(test, done) { 209 | var original = []; 210 | test.equal(Triggers.applyFilters(original), original); 211 | done(); 212 | }); 213 | 214 | Tinytest.addAsync( 215 | 'Triggers - applyFilters - single trigger to array', 216 | function(test, done) { 217 | var original = function() {}; 218 | test.equal(Triggers.applyFilters(original)[0], original); 219 | done(); 220 | }); 221 | 222 | Tinytest.addAsync( 223 | 'Triggers - applyFilters - only and except both', 224 | function(test, done) { 225 | var original = []; 226 | try { 227 | Triggers.applyFilters(original, {only: [], except: []}); 228 | } catch(ex) { 229 | test.isTrue(/only and except/.test(ex.message)); 230 | done(); 231 | } 232 | }); 233 | 234 | Tinytest.addAsync( 235 | 'Triggers - applyFilters - only is not an array', 236 | function(test, done) { 237 | var original = []; 238 | try { 239 | Triggers.applyFilters(original, {only: "name"}); 240 | } catch(ex) { 241 | test.isTrue(/to be an array/.test(ex.message)); 242 | done(); 243 | } 244 | }); 245 | 246 | Tinytest.addAsync( 247 | 'Triggers - applyFilters - except is not an array', 248 | function(test, done) { 249 | var original = []; 250 | try { 251 | Triggers.applyFilters(original, {except: "name"}); 252 | } catch(ex) { 253 | test.isTrue(/to be an array/.test(ex.message)); 254 | done(); 255 | } 256 | }); 257 | 258 | Tinytest.addAsync( 259 | 'Triggers - applyFilters - unsupported filter', 260 | function(test, done) { 261 | var original = []; 262 | try { 263 | Triggers.applyFilters(original, {wowFilter: []}); 264 | } catch(ex) { 265 | test.isTrue(/not supported/.test(ex.message)); 266 | done(); 267 | } 268 | }); 269 | 270 | Tinytest.addAsync( 271 | 'Triggers - applyFilters - just only filter', 272 | function(test, done) { 273 | var bounded = Triggers.applyFilters(done, {only: ["abc"]}); 274 | bounded[0]({route: {name: "abc"}}); 275 | }); 276 | 277 | Tinytest.addAsync( 278 | 'Triggers - applyFilters - just except filter', 279 | function(test, done) { 280 | var bounded = Triggers.applyFilters(done, {except: ["abc"]}); 281 | bounded[0]({route: {name: "some-other"}}); 282 | }); 283 | 284 | function MakeTriggers(count, store) { 285 | var triggers = []; 286 | 287 | function addTrigger(no) { 288 | triggers.push(function() { 289 | store.push(no); 290 | }); 291 | } 292 | 293 | for(var lc=0; lc(.*)<\/script/)[1]; 37 | return InjectData._decode(encodedData)['fast-render-data']; 38 | } -------------------------------------------------------------------------------- /test/server/plugins/fast_render.js: -------------------------------------------------------------------------------- 1 | Tinytest.add('Server - Fast Render - fast render supported route', function (test) { 2 | var expectedFastRenderCollData = [ 3 | [{_id: "two", aa: 20}, {_id: "one", aa: 10}] 4 | ]; 5 | 6 | var data = GetFRData('/the-fast-render-route'); 7 | test.equal(data.collectionData['fast-render-coll'], expectedFastRenderCollData); 8 | }); 9 | 10 | Tinytest.add('Server - Fast Render - fast render supported route with params', function (test) { 11 | var expectedFastRenderCollData = [ 12 | [{ 13 | _id: "one", 14 | params: {id: 'the-id'}, 15 | queryParams: {aa: "20"} 16 | }] 17 | ]; 18 | 19 | var data = GetFRData('/the-fast-render-route-params/the-id?aa=20'); 20 | test.equal(data.collectionData['fast-render-coll'], expectedFastRenderCollData); 21 | }); 22 | 23 | Tinytest.add('Server - Fast Render - no fast render supported route', function (test) { 24 | var data = GetFRData('/no-fast-render'); 25 | test.equal(data.collectionData, {}); 26 | }); 27 | 28 | Tinytest.add('Server - Fast Render - with group routes', function (test) { 29 | var expectedFastRenderCollData = [ 30 | [{_id: "two", aa: 20}, {_id: "one", aa: 10}] 31 | ]; 32 | 33 | var data = GetFRData('/fr/have-fr'); 34 | test.equal(data.collectionData['fast-render-coll'], expectedFastRenderCollData); 35 | }); --------------------------------------------------------------------------------