discoveryjs/discovery
Hackable JSON discovery tool
repo name | discoveryjs/discovery |
repo link | https://github.com/discoveryjs/discovery |
homepage | https://discoveryjs.github.io/discovery/ |
language | JavaScript |
size (curr.) | 1599 kB |
stars (curr.) | 180 |
created | 2018-11-20 |
license | MIT License |
Discovery
Hackable JSON discovery tool
Documentation in progress…
Articles
Examples
Related projects
- Discovery CLI – CLI tools to serve & build projects based on Discovery.js
- JsonDiscovery – Chrome/Firefox browser extension built on Discovery which allows to discover a JSON document and make reports
- Jora – data query language
- Jora CLI – a tool to process JSON data using Jora in command line interface
Plugins
Install
> npm install @discoveryjs/discovery
Base concepts
Model goes through data->prepare->render
chain. Data can be modified with prepare
function and rendered by various views and its combinations.
View is a function(el, config, data, context) where:
el
- DOM-element in which view will be renderedconfig
- configuration of viewdata
- data to rendercontext
- contains of model data, metaifo (createdAt, etc), router (optional), any user defined or view defined additional data
Page is a root view function(el, data, context). Similar to view it has all of its arguments except config.
Page
To define a page you should call discovery.page.define(pageId, render(el, data, context), options)
where:
pageId
- unique page identifierrender
- page render function described aboveoptions
- object with options:reuseEl
- do not clear container before render page (skiped for first render or pageId change)init
- invokes on first page render or pageId changekeepScrollOffset
- dont scroll to page top ifpageId
didn’t changed since last page renderencodeParams
decodeParams
Other handy methods for working with page:
discovery.renderPage()
discovery.setPage(pageId, pageRef, renderParam)
,discovery.setPageParams(renderParams)
triggersrenderPage()
discovery.getPageContext()
gets context of page
Special pages
There are some built-in special pages:
default
report
not-found
You can override this pages with page.define()
method
View
To define new view just call discovery.view.define(viewId, render, options)
where:
viewId
- unique view identifierrender
- function(el, config, data, context) or view definition objectoptions
- object with following fields:tag
- a tag name for a view container element. When value isfalse
ornull
thenDocumentFragment
is used as a container
You can render your view with discovery.view.render(el, view, data, context)
where:
view
can be stringviewId
,ViewDefinition
orArray<ViewDefinition>
viewId will expand to{ view: viewId }
.
Also you can specify
view
asviewId:query
, that will give you handy shortcut to{ view: viewId, data: query }
viewDefinition
object with view definition settings:view
: stringviewId
when
: query, will be coerced to Boolean (but empty arrays and objects will coerce tofalse
)data
: query (will be described later)- Also there can be view specific additional fields
Special views
Also special built-in sidebar
view is available you can override this view with view.define
Queries
As a query you can specify string which will be processed by Jora so your data will be a result of following flow:
jora(value) -> function(data, context) -> data
Or you can use function(data, context)
as query as well. Also you can use any data as query right away.
License
MIT