### Navigating with Query Parameters in FlowRouter Source: https://github.com/kadirahq/flow-router/blob/master/README.md This example shows how to navigate to a FlowRouter route that includes query parameters. When this URL is visited, the `action` function of the corresponding route will receive `comments` and `color` in its `queryParams` object. ```javascript FlowRouter.go('/blog/my-post?comments=on&color=dark'); ``` -------------------------------- ### Stopping Route Action with FlowRouter Triggers Source: https://github.com/kadirahq/flow-router/blob/master/README.md This snippet demonstrates how to use the `stop` function within a `triggersEnter` callback to prevent the route's `action` from executing. In this example, a `localeCheck` trigger validates a URL parameter; if the locale is invalid, a 'notFound' layout is rendered, and the `stop()` function is called to halt further route processing. ```javascript var localeGroup = FlowRouter.group({ prefix: '/:locale?', triggersEnter: [localeCheck] }); localeGroup.route('/login', { action: function (params, queryParams) { BlazeLayout.render('componentLayout', {content: 'login'}); } }); function localeCheck(context, redirect, stop) { var locale = context.params.locale; if (locale !== undefined && locale !== 'fr') { BlazeLayout.render('notFound'); stop(); } } ``` -------------------------------- ### Getting Current Route Name Reactively with FlowRouter.getRouteName (JavaScript) Source: https://github.com/kadirahq/flow-router/blob/master/README.md This reactive function returns the name of the currently active route. It can be used within a reactive context, such as a `Tracker.autorun`, to automatically update when the route changes. ```JavaScript Tracker.autorun(function() { var routeName = FlowRouter.getRouteName(); console.log("Current route name is: ", routeName); }); ``` -------------------------------- ### Navigating Programmatically with FlowRouter.go Source: https://github.com/kadirahq/flow-router/blob/master/README.md This command demonstrates how to programmatically navigate to a specific route using `FlowRouter.go()`. It simulates a browser visit to `/blog/my-post-id`, triggering the corresponding route's action function. ```javascript FlowRouter.go('/blog/my-post-id'); ``` -------------------------------- ### Enabling Hashbang URLs in FlowRouter (JavaScript) Source: https://github.com/kadirahq/flow-router/blob/master/README.md This JavaScript snippet demonstrates how to enable hashbang URLs (e.g., `mydomain.com/#!/mypath`) in FlowRouter. It first calls `FlowRouter.wait()` to pause routing until the application is ready, then initializes FlowRouter with the `hashbang: true` option inside a callback that executes when the app is fully loaded. ```javascript // file: app.js FlowRouter.wait(); WhenEverYourAppIsReady(function() { FlowRouter.initialize({hashbang: true}); }); ``` -------------------------------- ### Defining a Simple FlowRouter Route with Params and Query Params Source: https://github.com/kadirahq/flow-router/blob/master/README.md This snippet illustrates the basic syntax for defining a FlowRouter route, including capturing URL parameters (`:postId`) and accessing query parameters. The `action` function receives `params` and `queryParams` objects, and an optional `name` can be assigned to the route for easier reference. ```javascript FlowRouter.route('/blog/:postId', { // do some action for this route action: function(params, queryParams) { console.log("Params:", params); console.log("Query Params:", queryParams); }, name: "" // optional }); ``` -------------------------------- ### Defining a Basic FlowRouter Route Source: https://github.com/kadirahq/flow-router/blob/master/README.md This snippet defines a basic route for a blog post, capturing a `postId` parameter from the URL. The `action` function is executed when the route is visited, logging the extracted `postId` to the console. It's recommended to place this in `lib/router.js`. ```javascript FlowRouter.route('/blog/:postId', { action: function(params, queryParams) { console.log("Yeah! We are on the post:", params.postId); } }); ``` -------------------------------- ### Navigating to a Path with FlowRouter.go (JavaScript) Source: https://github.com/kadirahq/flow-router/blob/master/README.md This function navigates the user to a specified path. It internally uses `FlowRouter.path` to construct the URL based on the provided path definition, parameters, and query parameters, then performs the re-routing. ```JavaScript FlowRouter.go("/blog"); ``` -------------------------------- ### Checking Subscription Readiness with Callback (JavaScript) Source: https://github.com/kadirahq/flow-router/blob/master/README.md For non-reactive contexts like event handlers, `FlowRouter.subsReady()` can accept a callback function. This callback executes once the specified subscription (or all subscriptions if no name is provided) becomes ready, allowing for actions to be performed upon subscription completion. ```JavaScript Template.myTemplate.events({ "click #id": function(){ FlowRouter.subsReady("myPost", function() { // do something }); } }); ``` -------------------------------- ### Checking Subscription Readiness with FlowRouter.subsReady (JavaScript) Source: https://github.com/kadirahq/flow-router/blob/master/README.md This reactive function allows you to check the readiness status of registered subscriptions. When called with a specific subscription name, it returns `true` if that subscription is ready. When called without arguments, it returns `true` if all registered subscriptions are ready. It's ideal for showing loading states in templates. ```JavaScript Tracker.autorun(function() { console.log("Is myPost ready?:", FlowRouter.subsReady("myPost")); console.log("Are all subscriptions ready?:", FlowRouter.subsReady()); }); ``` -------------------------------- ### Controlling FlowRouter Initialization (JavaScript) Source: https://github.com/kadirahq/flow-router/blob/master/README.md By default, FlowRouter initializes on `Meteor.startup()`. For apps requiring custom initialization order, `FlowRouter.wait()` prevents automatic initialization. `FlowRouter.initialize()` must then be called manually when the application is ready to begin routing. ```JavaScript // file: app.js FlowRouter.wait(); WhenEverYourAppIsReady(function() { FlowRouter.initialize(); }); ``` -------------------------------- ### Defining and Using FlowRouter Group Routes Source: https://github.com/kadirahq/flow-router/blob/master/README.md This snippet demonstrates how to define a group of routes using `FlowRouter.group()`, applying a common `prefix`, `name`, and `triggersEnter` to all routes within the group. It then defines two routes (`/` and `/posts`) under this `adminRoutes` group, showcasing how group-level and individual route-level triggers interact, and how to render layouts with BlazeLayout. ```javascript var adminRoutes = FlowRouter.group({ prefix: '/admin', name: 'admin', triggersEnter: [function(context, redirect) { console.log('running group triggers'); }] }); // handling /admin route adminRoutes.route('/', { action: function() { BlazeLayout.render('componentLayout', {content: 'admin'}); }, triggersEnter: [function(context, redirect) { console.log('running /admin trigger'); }] }); // handling /admin/posts adminRoutes.route('/posts', { action: function() { BlazeLayout.render('componentLayout', {content: 'posts'}); } }); ``` -------------------------------- ### Defining Enter and Exit Triggers for a FlowRouter Route Source: https://github.com/kadirahq/flow-router/blob/master/README.md This snippet illustrates how to define `triggersEnter` and `triggersExit` for a specific FlowRouter route. `triggersEnter` functions execute before the route's `action`, while `triggersExit` functions run when navigating away from the route. Both receive a `context` object, useful for tracking or other pre/post-action logic, such as Mixpanel tracking. ```javascript FlowRouter.route('/home', { // calls just before the action triggersEnter: [trackRouteEntry], action: function() { // do something you like }, // calls when we decide to move to another route // but calls before the next route started triggersExit: [trackRouteClose] }); function trackRouteEntry(context) { // context is the output of `FlowRouter.current()` Mixpanel.track("visit-to-home", context.queryParams); } function trackRouteClose(context) { Mixpanel.track("move-from-home", context.queryParams); } ``` -------------------------------- ### Defining Global FlowRouter Triggers with Filtering Source: https://github.com/kadirahq/flow-router/blob/master/README.md This snippet demonstrates how to define global `triggers.enter` and `triggers.exit` that apply to all routes. It also shows how to filter these global triggers using `only` (to apply to specific routes) or `except` (to exclude specific routes), providing fine-grained control over trigger execution across the application. ```javascript FlowRouter.triggers.enter([cb1, cb2]); FlowRouter.triggers.exit([cb1, cb2]); // filtering FlowRouter.triggers.enter([trackRouteEntry], {only: ["home"]}); FlowRouter.triggers.exit([trackRouteExit], {except: ["home"]}); ``` -------------------------------- ### Rendering Layouts with BlazeLayout in FlowRouter Action Source: https://github.com/kadirahq/flow-router/blob/master/README.md This snippet demonstrates how to integrate a layout manager like BlazeLayout within a FlowRouter route's `action` function. It shows rendering a `mainLayout` template and passing data (`area: 'blog'`) to it, allowing FlowRouter to manage routing while BlazeLayout handles the view rendering. ```javascript FlowRouter.route('/blog/:postId', { action: function(params) { BlazeLayout.render("mainLayout", {area: "blog"}); } }); ``` -------------------------------- ### Registering Route Callback with FlowRouter.onRouteRegister (JavaScript) Source: https://github.com/kadirahq/flow-router/blob/master/README.md Designed for add-on developers, this API allows registering a callback function that executes whenever a new route is registered. The callback receives a `route` object containing `pathDef`, `name`, and `options` (custom fields from the route definition), enabling custom functionality based on route registration. ```JavaScript FlowRouter.onRouteRegister(function(route) { // do anything with the route object console.log(route); }); ``` ```JavaScript FlowRouter.route('/blog/:post', { name: 'postList', triggersEnter: [function() {}], subscriptions: function() {}, action: function() {}, triggersExit: [function() {}], customField: 'customName' }); ``` -------------------------------- ### Adding HTML5 History Polyfill for IE9 Support (Shell) Source: https://github.com/kadirahq/flow-router/blob/master/README.md This shell command adds the `tomwasd:history-polyfill` Meteor package, which is required to enable HTML5 history API support for FlowRouter in Internet Explorer 9. This polyfill is not included by default as most applications do not require it. ```shell meteor add tomwasd:history-polyfill ``` -------------------------------- ### Accessing Current Router State with FlowRouter.current (JavaScript) Source: https://github.com/kadirahq/flow-router/blob/master/README.md This non-reactive API provides a snapshot of the router's current state, including the path, parameters, query parameters, and route definition. For reactive updates, `FlowRouter.watchPathChange()` should be used instead. ```JavaScript // route def: /apps/:appId // url: /apps/this-is-my-app?show=yes&color=red var current = FlowRouter.current(); console.log(current); // prints following object // { // path: "/apps/this-is-my-app?show=yes&color=red", // params: {appId: "this-is-my-app"}, // queryParams: {show: "yes", color: "red"} // route: {pathDef: "/apps/:appId", name: "name-of-the-route"} // } ``` -------------------------------- ### Registering Route-Specific Subscriptions with FlowRouter (JavaScript) Source: https://github.com/kadirahq/flow-router/blob/master/README.md FlowRouter allows registering subscriptions directly within a route definition's `subscriptions` function. This function receives `params` and `queryParams` and uses `this.register()` to associate a subscription with the current route. FlowRouter only registers subscriptions; it does not wait for them to be ready. ```JavaScript FlowRouter.route('/blog/:postId', { subscriptions: function(params, queryParams) { this.register('myPost', Meteor.subscribe('blogPost', params.postId)); } }); ``` -------------------------------- ### Configuring FlowRouter Not Found Route Source: https://github.com/kadirahq/flow-router/blob/master/README.md This snippet shows how to define a global 'not found' route in FlowRouter, which is activated when no other route matches the current URL. It allows for custom `subscriptions` and an `action` function to handle the display of a 404 page or other fallback content. ```javascript FlowRouter.notFound = { // Subscriptions registered here don't have Fast Render support. subscriptions: function() { }, action: function() { } }; ``` -------------------------------- ### Adding FlowRouter to Meteor Project Source: https://github.com/kadirahq/flow-router/blob/master/README.md This command adds the FlowRouter package to your Meteor application, making its routing capabilities available for use. It's the first step to integrate FlowRouter into your project. ```shell meteor add kadira:flow-router ``` -------------------------------- ### Registering Global Subscriptions with FlowRouter (JavaScript) Source: https://github.com/kadirahq/flow-router/blob/master/README.md Global subscriptions can be registered by assigning a function to `FlowRouter.subscriptions`. These subscriptions run on every route. It's important to use distinct names when registering subscriptions to avoid conflicts. ```JavaScript FlowRouter.subscriptions = function() { this.register('myCourses', Meteor.subscribe('courses')); }; ``` -------------------------------- ### Generating Paths with FlowRouter.path (JavaScript) Source: https://github.com/kadirahq/flow-router/blob/master/README.md This method generates a URL path from a given path definition, optionally incorporating dynamic parameters and query parameters. Special characters in `params` and `queryParams` are automatically URL encoded. If no parameters or query parameters are provided, it returns the `pathDef` as is. ```JavaScript var pathDef = "/blog/:cat/:id"; var params = {cat: "met eor", id: "abc"}; var queryParams = {show: "y+e=s", color: "black"}; var path = FlowRouter.path(pathDef, params, queryParams); console.log(path); // prints "/blog/met%20eor/abc?show=y%2Be%3Ds&color=black" ``` ```JavaScript FlowRouter.route("/blog/:cat/:id", { name: "blogPostRoute", action: function(params) { //... } }) var params = {cat: "meteor", id: "abc"}; var queryParams = {show: "yes", color: "black"}; var path = FlowRouter.path("blogPostRoute", params, queryParams); console.log(path); // prints "/blog/meteor/abc?show=yes&color=black" ``` -------------------------------- ### Retrieving URL Parameters Reactively with FlowRouter.getParam Source: https://github.com/kadirahq/flow-router/blob/master/README.md This snippet demonstrates the use of `FlowRouter.getParam()`, a reactive function that retrieves the value of a specified URL parameter from the current route. It's useful for dynamically accessing route-specific data, such as an `appId`, within reactive contexts. ```javascript // route def: /apps/:appId // url: /apps/this-is-my-app var appId = FlowRouter.getParam("appId"); console.log(appId); // prints "this-is-my-app" ``` -------------------------------- ### Redirecting Routes Using FlowRouter Triggers Source: https://github.com/kadirahq/flow-router/blob/master/README.md This snippet illustrates how to perform a client-side redirect within a `triggersEnter` function. The `redirect` function, passed as the second argument to the trigger callback, allows immediate navigation to a different path, preventing the original route's `action` from being executed. ```javascript FlowRouter.route('/', { triggersEnter: [function(context, redirect) { redirect('/some-other-path'); }], action: function(_params) { throw new Error("this should not get called"); } }); ``` -------------------------------- ### Modifying Browser History with FlowRouter.withReplaceState (JavaScript) Source: https://github.com/kadirahq/flow-router/blob/master/README.md This method executes a function (`fn`) while ensuring that any route changes made within `fn` (e.g., via `FlowRouter.go` or `FlowRouter.setParams`) replace the current entry in the browser history instead of adding a new one. This prevents polluting the browser's back stack. ```JavaScript FlowRouter.setParams({id: "the-id-1"}); FlowRouter.setParams({id: "the-id-2"}); FlowRouter.setParams({id: "the-id-3"}); ``` ```JavaScript FlowRouter.withReplaceState(function() { FlowRouter.setParams({id: "the-id-1"}); FlowRouter.setParams({id: "the-id-2"}); FlowRouter.setParams({id: "the-id-3"}); }); ``` -------------------------------- ### Reactively Watching Path Changes with FlowRouter.watchPathChange (JavaScript) Source: https://github.com/kadirahq/flow-router/blob/master/README.md This function allows you to reactively monitor changes in the URL path. It's suitable for scenarios where you need to perform actions whenever the path changes, and it can be used in conjunction with `FlowRouter.current()` to access the updated context. ```JavaScript Tracker.autorun(function() { FlowRouter.watchPathChange(); var currentContext = FlowRouter.current(); // do anything with the current context // or anything you wish }); ``` -------------------------------- ### Retrieving Query Parameters with FlowRouter.getQueryParam (JavaScript) Source: https://github.com/kadirahq/flow-router/blob/master/README.md This reactive function allows you to extract a specific value from the URL's query string. It takes the `queryStringKey` as an argument and returns the corresponding value. Changes to the query parameter will reactively update the returned value. ```JavaScript // route def: /apps/:appId // url: /apps/this-is-my-app?show=yes&color=red var color = FlowRouter.getQueryParam("color"); console.log(color); // prints "red" ``` -------------------------------- ### Defining Triggers for a FlowRouter Group Route Source: https://github.com/kadirahq/flow-router/blob/master/README.md This snippet shows how to apply `triggersEnter` and `triggersExit` at the group level in FlowRouter. Any route defined within this `adminRoutes` group will inherit and execute these specified triggers, providing a centralized way to manage common behaviors for a set of related routes. ```javascript var adminRoutes = FlowRouter.group({ prefix: '/admin', triggersEnter: [trackRouteEntry], triggersExit: [trackRouteEntry] }); ``` -------------------------------- ### Conditional Subscription Registration for Fast Render (JavaScript) Source: https://github.com/kadirahq/flow-router/blob/master/README.md To exclude specific subscriptions from Fast Render, you can wrap their registration within an `if(Meteor.isClient)` block inside the route's `subscriptions` function. This ensures that only subscriptions intended for Fast Render are processed on the server, while client-only subscriptions are handled exclusively on the client. ```JavaScript FlowRouter.route('/blog/:postId', { subscriptions: function(params, queryParams) { // using Fast Render this.register('myPost', Meteor.subscribe('blogPost', params.postId)); // not using Fast Render if(Meteor.isClient) { this.register('data', Meteor.subscribe('bootstrap-data'); } } }); ``` -------------------------------- ### Defining Nested FlowRouter Group Routes Source: https://github.com/kadirahq/flow-router/blob/master/README.md This snippet illustrates the creation of nested route groups in FlowRouter. An `adminRoutes` group is defined, and then a `superAdminRoutes` group is created as a child of `adminRoutes`, inheriting and extending its prefix. This allows for hierarchical organization of routes, such as `/admin/super/post`. ```javascript var adminRoutes = FlowRouter.group({ prefix: "/admin", name: "admin" }); var superAdminRoutes = adminRoutes.group({ prefix: "/super", name: "superadmin" }); // handling /admin/super/post superAdminRoutes.route('/post', { action: function() { } }); ``` -------------------------------- ### Accessing Route Parameters with Iron Router's current() (JavaScript) Source: https://github.com/kadirahq/flow-router/blob/master/README.md This JavaScript helper function, intended for Iron Router, retrieves the `appId` parameter from the current route using `Router.current().params.appId`. This approach can lead to unpredictable re-runs of the helper if other parts of the URL (like `:section` or query parameters) change, even if `appId` remains constant. ```javascript Templates['foo'].helpers({ "someData": function() { var appId = Router.current().params.appId; return doSomething(appId); } }); ``` -------------------------------- ### Accessing Route Parameters with FlowRouter's getParam() (JavaScript) Source: https://github.com/kadirahq/flow-router/blob/master/README.md This JavaScript helper function, designed for FlowRouter, safely retrieves the `appId` parameter using `FlowRouter.getParam('appId')`. Unlike Iron Router's `Router.current()`, this method ensures the helper only re-runs if the `appId` itself changes, preventing unnecessary re-renders and improving predictability. ```javascript Templates['foo'].helpers({ "someData": function() { var appId = FlowRouter.getParam('appId'); return doSomething(appId); } }); ``` -------------------------------- ### Updating Query Parameters with FlowRouter.setQueryParams (JavaScript) Source: https://github.com/kadirahq/flow-router/blob/master/README.md Similar to `FlowRouter.setParams`, this method updates the current route's query string parameters. To remove a query parameter, set its value to `null` within the `newQueryParams` object. ```JavaScript FlowRouter.setQueryParams({paramToRemove: null}); ``` -------------------------------- ### Updating Route Parameters with FlowRouter.setParams (JavaScript) Source: https://github.com/kadirahq/flow-router/blob/master/README.md This method updates the current route's parameters with `newParams` and triggers a re-route to the modified path. It's useful for changing parts of the URL dynamically without a full page reload. ```JavaScript // route def: /apps/:appId // url: /apps/this-is-my-app?show=yes&color=red FlowRouter.setParams({appId: "new-id"}); // Then the user will be redirected to the following path // /apps/new-id?show=yes&color=red ``` -------------------------------- ### Accessing Parent Group Name of Nested Routes Source: https://github.com/kadirahq/flow-router/blob/master/README.md For routes within nested groups, this snippet shows how to access the name of the immediate parent group. This provides a way to traverse the group hierarchy and apply logic based on higher-level group affiliations. ```javascript FlowRouter.current().route.group.parent.name ``` -------------------------------- ### Accessing Current Route's Group Name Source: https://github.com/kadirahq/flow-router/blob/master/README.md This line of code demonstrates how to retrieve the name of the group to which the currently active route belongs. This is useful for conditional logic based on the route's organizational group, such as 'admin' or 'public'. ```javascript FlowRouter.current().route.group.name ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.