Cape.RoutingMapper - API Reference

collection() - namespace() - new() - many() - member() - one() - page() - root() - view()

#collection()

Usage

  • collection(name)

Adds a collection route (a route which deals with multiple items).

Example

var router = new Cape.Router();
router.draw(function(m) {
  m.many('articles', function(m) {
    m.collection 'draft'
  });
})

This defines a route from articles/draft to Articles.Draft.

The #collection() method must be called within a block that defines a plural resource.

#namespace()

Usage

  • namespace(name, function)

Introduces a function tha defines resources under the specified namespace.

var router = new Cape.Router();
router.draw(function(m) {
  m.namespace('admin', function(m) {
    m.many('articles');
  })
})

The above code defines four routes. See the table below:

Hash pattern Container Component Namespace Resource Action
admin/articles Admin.Articles List admin articles index
admin/articles/:id Admin.Articles Item admin articles show
admin/articles/new Admin.Articles Form admin articles new
admin/articles/:id/edit Admin.Articles Form admin articles edit

#new()

Usage

  • new(name)

Adds an alternate new action to a resource, which can be plural or singular.

var router = new Cape.Router();
router.draw(function(m) {
  m.one('account', function(m) {
    m.new 'preview'
  });
  m.many('articles', function(m) {
    m.new 'preview'
  });
})

This defines two routes:

  • From account/new/preview to Account.Preview.
  • From articles/new/preview to Articles.Preview.

#many()

Usage

  • many(name)

Defines four routes for a plural resource.

Example

var router = new Cape.Router();
router.draw(function(m) {
  m.many('articles');
})

The above code defines four routes. See the table below:

Hash pattern Container Component Resource Action
articles Articles List articles index
articles/:id Articles Item articles show
articles/new Articles Form articles new
articles/:id/edit Articles Form articles edit

Usage

  • many(name, { only: [ action1, action2, … ] })

Adds routes for action1, action2, … to a plural resource. The value of action1, action2, … must be “index”, “show”, “new” or “edit”.

Example

var router = new Cape.Router();
router.draw(function(m) {
  m.many('articles', { only: [ 'index', 'show' ] });
})

This defines two routes:

  • From articles/ to Articles.List.
  • From articles/:id to Articles.Item.

Usage

  • many(name, { except: [ action1, action2, … ] })

Adds basic routes excepting action1, action2, … to a plural resource. The value of action1, action2, … must be “index”, “show”, “new” or “edit”.

Example

var router = new Cape.Router();
router.draw(function(m) {
  m.many('articles', { except: [ 'new', 'edit' ] });
})

This defines two routes:

  • From articles/ to Articles.List.
  • From articles/:id to Articles.Item.

#member()

Usage

  • member(name)

Adds a member route (a route which deals with a specific item).

Example.

var router = new Cape.Router();
router.draw(function(m) {
  m.many('articles', function(m) {
    m.member 'info'
  });
})

This defines a route from articles/:id/info to Articles.Info.

The #member() method must be called within a block that defines a plural resource.

#one()

Usage

  • one(name)

Defines four routes for a singular resource.

Example

var router = new Cape.Router();
router.draw(function(m) {
  m.one('account');
})

The above code defines four routes. See the table below:

Hash pattern Container Component Resource Action
account Account Content account show
account/new Account Form account new
account/edit Account Form account edit

Usage

  • one(name, { only: [ action1, action2, … ] })

Adds routes for action1, action2, … to a singular resource. The value of action1, action2, … must be “show”, “new” or “edit”.

Example

var router = new Cape.Router();
router.draw(function(m) {
  m.new('account', { only: [ 'show' ] });
})

This defines a route:

  • From account/ to Account.Content.

Usage

  • one(name, { except: [ action1, action2, … ] })

Adds basic routes excepting action1, action2, … to a singular resource. The value of action1, action2, … must be “show”, “new” or “edit”.

Example

var router = new Cape.Router();
router.draw(function(m) {
  m.one('account', { except: [ 'new' ] });
})

This defines two routes:

  • From account to Account.Content.
  • From account/edit to Account.Form.

#page()

Usage

  • page(hashPattern, componentClassPath)

Defines a route from hashPattern to componentClassPath.

Example

var router = new Cape.Router();
router.draw(function(m) {
  m.page('dashboard', 'dashboard');
  m.page('about', 'docs.about');
  m.page('help/:name', 'docs.help');
})

Usage

  • page(hashPattern, componentClassPath, constraints)

Defines a route from hashPattern to componentClassPath with constraints on the parameters, which are specified by regular expression string.

Example

var router = new Cape.Router();
router.draw(function(m) {
  m.page('blog/:year/:month', 'blog.articles',
    { year: '201\\d', month: '(:?0[1-9]|1[012])' });
})

Note that you should put :? to make parentheses non-capturing parentheses .

Usage

  • page(hashPattern) 1.4

Defines a route from hashPattern to the component class path which is consturcted from hashPattern by replacing all slashes with dots.

Example

var router = new Cape.Router();
router.draw(function(m) {
  m.page('dashboard');
  m.page('docs/about');
  // The above codes have same meanings as the following alternatives:
  //   m.page('dashboard', 'dashboard');
  //   m.page('docs/about', 'docs.about');
})

#root()

Usage

  • root(componentClassPath)

Defines a route from “” to componentClassPath.

Example

var router = new Cape.Router();
router.draw(function(m) {
  root('Dashboard')
})

#view()

Usage

  • view(name)

Adds a custom route to a singular resource.

Example

var router = new Cape.Router();
router.draw(function(m) {
  m.one('account', function(m) {
    m.view 'image'
  });
})

This defines a route from account/image to Account.Image.

The #view() method must be called within a block that defines a singular resource.