1.1.2 • Published 4 years ago

marionette.routing v1.1.2

Weekly downloads
24
License
MIT
Repository
github
Last release
4 years ago

Marionette Routing

NPM version NPM downloads Build Status Coverage Status Dependency Status

An advanced router for MarionetteJS applications

Features

    ✓ Nested routes / states / rendering     ✓ Handle asynchronous operations     ✓ Lazy loading of routes with code splitting     ✓ Expose route events through Radio     ✓ Implement route context for scoped messaging     ✓ API interface semantics similar to MarionetteJS one     ✓ Inherits most of Cherrytree features

Installation

$ npm install --save marionette.routing

Requires MarionetteJS v4+, Radio v2+, underscore v1.8+ as peer dependencies

Requires a ES6 Promise implementation attached in window (native or polyfill)

Usage

Define a Route class

import {Route} from 'marionette.routing';
import {Contacts} from '../entities';
import ContactsView from './view';

export default Route.extend({
  activate(){
    const contactsPromise = Radio.channel('api').request('getContactList');
    return contactsPromise.then(contactsData => {
      this.contacts = new Contacts(contactsData)
    });
  },

  viewClass: ContactsView,

  viewOptions() {
    return {
      contacts: this.contacts
    }
  }
})

Configure and start the router

import { Router } from 'marionette.routing';
import ContactsRoute from './contacts/route';
import LoginView from './login/view';
import Mn from 'backbone.marionette';
import Radio from 'backbone.radio';

//create the router
let router = new Router(
  { log: true, logError: true }, // options passed to Cherrytree
  '#main' // the element / Marionette Region where the root routes will be rendered
);

//define the routes
router.map(function (route) {
  route('application', {path: '/', abstract: true}, function () {
    route('contacts', {routeClass: ContactsRoute})
    route('login', {viewClass: LoginView})
  })
});

//start listening to URL changes
router.listen();

//listen to events using Radio
Radio.channel('router').on('before:activate', function(transition, route) {
  let isAuthenticate = checkAuth();
  if (!isAuthenticate && route.requiresAuth) {
    transition.redirectTo('login');
  }
})

Documentation

Examples

Related Projects

  • Cherrytree — The router library used by Marionette Routing under the hood

License

Copyright © 2016 Luiz Américo Pereira Câmara. This source code is licensed under the MIT license found in the LICENSE.txt file. The documentation to the project is licensed under the CC BY-SA 4.0 license.

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.12.0

5 years ago

0.11.0

5 years ago

0.10.1

5 years ago

0.10.0

5 years ago

0.9.1

6 years ago

0.9.0

6 years ago

0.8.3

6 years ago

0.8.2

6 years ago

0.8.1

6 years ago

0.7.0

7 years ago

0.6.2

7 years ago

0.6.1

7 years ago

0.6.0

7 years ago

0.5.3

7 years ago

0.5.2

7 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.4.2

7 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.0

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago

0.0.1

7 years ago