0.17.2 • Published 6 years ago

dominiq v0.17.2

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

Dominiq

Travis CI Codecov npm

A happy medium between classic DOMs and upcoming new ES features:

  • listen() DOM events and extract data from them in Observable way.
  • App class provides an easy way to handle states and actions:
    • app.commit() to mutate its state
    • app.dispatch() to call an action

Dominiq found a natural way to build an application with modern methods. We fully respect these native JavaScript features and just combined them:

Contents

  • Core concept (see below)
  • Basic usages (see below)
  • Installation
  • Extraction - Nested names / From events / Via Observables / Full or partial data / Event delegation
  • State - Set and get / Flat or nested / Computed properties / Input validations / Sanitization
  • Action - Dispatch / Mutation / Async operation / Separated code
  • Events - render / rendered / started / stopped / changed:*
  • APIs

Core concept

From such a DOM tree:

<body>
  <input name="first" value="John">
  <input name="last" value="Doe">
</body>

Extract the data:

const data = extract(document.body) // { first: "John", last: "Doe" }

Or, merge them into the state continuously in Observable way:

const state = { first: "", last: "" }
listen(document.body, "change") // Create event observable
  .subscribe(e => merge(state, toData(e))) // Extract the data

Basic usages

Prepare such a view file:

// view.js
import { html } from "lit-html/lib/lit-extended"
export default state => html`
  <h1>Hello ${ state.first }!</h1>
  <input name="first" value="${ state.first }">
  <input name="last" value="${ state.last }">
  <button name="submit">Click me!</button>
`

Note: In the example, we use lit-html as a HTML renderer, but you can choose any library/framework for it.

import { render } from "lit-html"
import { listen, App } from "dominiq"
import view from "./view.js"

const initialState = { first: "", last: "" }
const actions = {
  submit (state) { console.log(`Hello ${ state.first }!`) }
}
const dom = document.body
const app = new App({ initialState, actions })

// Listen <input> and commit changes into the state
listen(dom, "change").subscribe(app.commit)
// Listen <button> and dispatch actions
listen(dom, "click").subscribe(app.dispatch)
// Listen app and render the view
listen(app, "render").subscribe(state => render(view(state), dom))
app.start()

Note: listen() is just a thin helper and equivalent to RxJS's fromEvent()

License

MIT

0.17.2

6 years ago

0.17.1

6 years ago

0.17.0

6 years ago

0.16.3

6 years ago

0.16.2

6 years ago

0.16.0

6 years ago

0.15.0

6 years ago

0.14.0

6 years ago

0.13.0

6 years ago

0.12.0

6 years ago

0.11.3

6 years ago

0.11.2

6 years ago

0.11.1

6 years ago

0.10.1

6 years ago

0.10.0

6 years ago

0.9.0

6 years ago

0.8.1

6 years ago

0.8.0

6 years ago

0.7.3

6 years ago

0.7.2

6 years ago

0.7.1

6 years ago

0.7.0

6 years ago

0.5.0

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago