API Docs for: 0.3.0
Show:

PageRouter Class

Defined in: lib/mini-pages.js:90

The main mini-pages class. Manages creating pages, rendering and responding to url path changes. You can only create one PageRouter instance.

Constructor

PageRouter

(
  • [options]
)

Parameters:

  • [options] Object optional

    PageRouter options

    • [autoRender] Boolean optional

      Set false to render manually (i.e. when you use the {{renderPages}} Handlebars helper

    • [autoStart] Boolean optional

      Set to false to manually decide when to listen for url change events.

    • [defaults] Object optional

      Default options that will be applied to all PageRouter.Page instances.

Methods

_runWithInvocation

(
  • page
  • [context]
)
private

Creates a new PageRouter.PageInvocation and runs the page. Then set's the router's layout, template, nav and invocation object (the object you get when you call in your html.

Parameters:

  • page PageRouter.Page

    The page instance.

  • [context] Object optional

    An optional context object.

autoRender

()

Automatically render the router to the document.body. By default the router will use autoRender(). If you instead want to render the router's pages in a particular part of the DOM you can call render() instead and attach the document fragment manually. For example, the Handlebars helper {{renderPages}} does this.

autoStart

() chainable

Automatically start the router by adding a callback to Meteor.startup. The router is auto started by default.

go

(
  • path
  • [state]
)
chainable

Navigate to a given path.

Parameters:

  • path String

    The path to navigate to.

  • [state] Object optional

    A state object to pass along.

Example:

var router = new Meteor.PageRouter({});
router.go("/posts/1234", { someState: "" });

invocation

(
  • [value]
)
Object

Get or set the current invocation object. This is the object that is available in your template html by using the page handlebars helper like this: {{page.someProperty}}. The object gets its values from a before filter calling this.set("key", "value"). The value is stored in Session so that it's reactive and survives hot code pushes.

Parameters:

  • [value] Object optional

    If an object is passed it will be set, otherwise the current value will be returned.

Returns:

Object: Returns the current invocation object if no parameter is passed.

isRendered

() Boolean

Returns true of the render() method has been called at least once. Does not mean that anything has been attached to the dom.

Returns:

Boolean: True of the render() method has been called.

isStarted

() Boolean

Returns true of the start() method has been called

Returns:

Boolean: True if the start() method has been called

layout

(
  • [value]
)
String

Reactively get or set the current layout.

Parameters:

  • [value] String optional

    The name of the layout template.

Returns:

String: If no value passed, return the current layout template name.

layoutEquals

(
  • value
)
Boolean

Reactively test the current layout name for equality with the passed value.

Parameters:

  • value String

    The name of the layout template to test quality.

Returns:

Boolean: Returns true if the template names are equal.

match

(
  • path
  • [options]
)
PageRouter.Page

Create a new PageRouter.Page and return it.

Parameters:

  • path String

    The path to match.

  • [options] Object optional

    Options to pass to the PageRouter.Page constructor.

Returns:

Example:

// with options
var router = new Meteor.PageRouter({});
router.match("/posts/:_id", { to: "templateName" };

// no options
var router = new Meteor.PageRouter({});
router.match("/posts/:_id").to("templateName");

nav

(
  • value
)
String

Reactively get or set the current nav key.

Parameters:

  • value String

    The value of the nav key.

Returns:

String: If no value passed returns the current nav key.

navEquals

(
  • value
)
Boolean

Reactively test the current nav key for equality with the passed value.

Parameters:

  • value String

    The nav key to test for equality.

Returns:

Boolean: Returns true if the nav keys are equal.

pages

(
  • pages
)
chainable

Create a bunch of pages at once.

Parameters:

  • pages Object

    An object of page definitions where the key is a url path and the values are options to the PageRouter.Page constructor.

Example:

var router = new Meteor.PageRouter({});
router.pages({
 "/posts/:_id": {
    to: "templateName",
    layout: "layoutName",
    nav: "nav key",
    before: [ before callbacks ],
    as: "path helpers name"
 }
});

path

() String

Returns the router's current url path.

Returns:

String: The current url path.

render

() DocumentFragment | String

Creates two isolated regions. One for the layout and one for the template. Uses Spark's native reactivity to re-render the layout or template if the router's current layout or template changes.

Returns:

DocumentFragment | String: If called from within Meteor.render it returns a DocumentFragment, otherwise a string of html.

run

(
  • path
  • page
  • [context]
)
chainable

If the path is different from the current path, creates a new PageRouter.PageInvocation and runs the page with the new invocation.

Parameters:

  • path String

    The url path.

  • page PageRouter.Page

    A page instance.

  • [context] Object optional

    An optional context object.

start

() chainable

Start listening for pushState events from the browser and responding to url changes. Call this to manually start listening. If you call autoStart() this will automatically be called from within a Meteor.startup callback. You usually shouldn't need to call this method.

template

(
  • value
)
String

Reactively get or set the current template.

Parameters:

  • value String

    The name of the template.

Returns:

String: If no value passed returns the name of the current template.

templateEquals

(
  • value
)
Boolean

Reactively test the current template name for equality with the passed value.

Parameters:

  • value String

    The name of the template to test quality.

Returns:

Boolean: Returns true if the template names are equal.