Sha256: e6db25b86e1286d339a89a0d5fa9971a0ff22fc79768c171aa68c7cc08d849bd

Contents?: true

Size: 1.56 KB

Versions: 4

Compression:

Stored size: 1.56 KB

Contents

# Ovto::App

First of all, you need to define a subclass of `Ovto::App` and define `class State`,
`class Actions` and `class View` in it.

## Example

This is a smallest Ovto app.

```rb
require 'opal'
require 'ovto'

class MyApp < Ovto::App
  class State < Ovto::State
  end

  class Actions < Ovto::Actions
  end

  class View < Ovto::Component
    def render(state:)
      o 'input', type: 'button', value: 'Hello'
    end
  end
end

MyApp.run(id: 'ovto-view')
```

It renders a button and does nothing else. Let's have some fun:

```rb
require 'opal'
require 'ovto'

class MyApp < Ovto::App
  COLORS = ["red", "blue", "green"]

  class State < Ovto::State
    item :color_idx, default: 0
  end

  class Actions < Ovto::Actions
    def update_color(state:)
      new_idx = (state.color_idx + 1) % COLORS.length
      return {color_idx: new_idx}
    end
  end

  class View < Ovto::Component
    def render(state:)
      o 'input', {
        type: 'button',
        value: 'Hello',
        style: {background: COLORS[state.color_idx]},
        onclick: ->{ actions.update_color },
      }
    end
  end
end

MyApp.run(id: 'ovto-view')
```

Here we added `color_idx` to app state and `update_color` action to change it.
The button is updated to have the color indicated by `color_idx` and
now has `onclick` event handler which executes the action.

## Calling actions on startup

To invoke certain actions on app startup, define `MyApp#setup` and use `MyApp#actions`.

Example:

```rb
class MyApp < Ovto::App
  def setup
    actions.fetch_data()
  end

  ...
end

MyApp.run(id: 'ovto-view')
```

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ovto-0.2.3 book/api/app.md
ovto-0.2.2 book/api/app.md
ovto-0.2.1 book/api/app.md
ovto-0.2.0 book/api/app.md