Giter VIP home page Giter VIP logo

Comments (2)

KidkArolis avatar KidkArolis commented on August 28, 2024

It's not currently possible to extend route configuration itself dynamically. But in practise it might not be a problem. As long as you define the route map upfront, you can then still dynamically load parts of your app. We do this in production. For example:

// app.js
let cherrytree = require('cherrytree')
let resolvers = require('./routing/resolvers')
let loadCode = require('./routing/loadCode')

let router = cherrytree()
router.use(loadCode)
router.map(function (route) {
  route('application', { resolver: resolvers.application }, function () {
    route('logout')
    route('list', { resolver: resolvers.list }, function () {
      route('list.tab')
    })
    route('item', { resolver: resolvers.item }, function () {
      route('item.details')
    })
  })
})
// routing/resolvers.js
let resolvers = {}

resolvers.application = function (name, cb) {
  require.ensure([], function (require) {
    cb(require('app/modules/application/routes')[name])
  }, 'list-routes')
}

resolvers.list = function (name, cb) {
  require.ensure([], function (require) {
    cb(require('app/modules/list/routes')[name])
  }, 'list-routes')
}

resolvers.item = function (name, cb) {
  require.ensure([], function (require) {
    cb(require('app/modules/item/routes')[name])
  }, 'item-routes')
}
// app/modules/item/routes.js
module.exports = {
  'item': require('./itemComponent'),
  'item.details': require('./itemDetailsComponent')
}
// routing/loadCode.js
let when = require('when')

module.exports = function loadCode (transition) {
  return when.map(transition.routes, function (memo, route) {
    return resolve(route, transition.routes).then(function (Component) {
      route.component = Component
    })
  })
}

function resolve (route, routes) {
  let r = route
  while (r) {
    if (r.options.resolver) {
      // this route has a resolver configured
      return when.promise(resolve => r.options.resolver(name, resolve))
    } else {
      // walk up to the parent routes to find a resolver
      r = routes[routes.indexOf(r) - 1]
    }
  }
  throw new Error('Couldn\'t find a route resolver for ' + route.name)
}

And now webpack would build list page routes and item page routes into separate bundles and cherrytree would load those bundles dynamically as part of transitioning to those pages.

I'll have to take a closer look at react-router's API for this as they seem to have less boilerplate.

from cherrytree.

blikblum avatar blikblum commented on August 28, 2024

Many thanks for your work. I think this library has a right balance between functionality, flexibility and size.
Just wanted to know if the feature is already available but is not a deal breaker. Closing the issue

from cherrytree.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.