jondashkyle / nanopage

super handy utilities for traversing flat content state
Apache License 2.0
42 stars 4 forks source link

Dynamic argument #15

Open jongacnik opened 6 years ago

jongacnik commented 6 years ago

Allow a second argument into page to define a dynamic/fallback content entry. Example in context of choo:

var page = require('nanopage')

var p = page(state.href, {
  source: `/bundles/${state.params.id}.json`,
  loaded: false
})

This allows for pages to be added dynamically to state. Logic under the hood is something like:

function page (key, dynamic) {
  var content = state.content[key]

  if (!content && dynamic) {
    state.content[key] = dynamic
    content = state.content[key]
  }

  return content
}

That second param could also be a function:

var p = page(state.href, key => ({
  source: `/bundles/${state.params.id}.json`,
  loaded: false
}))

@s3ththompson