inesita-rb / inesita

Frontend web application framework in Ruby using Opal.
https://inesita.fazibear.me/
MIT License
259 stars 15 forks source link

Add ActionDispatcher #16

Closed olegantonyan closed 7 years ago

olegantonyan commented 8 years ago

Following Flux architecture. Also helps with https://github.com/inesita-rb/inesita/issues/15 Example usage:

# application.js.rb
require 'store'
require 'layout'
require 'router'
require 'dispatcher'

# fix headers
Browser::HTTP::Request::HEADERS.delete('X-Opal-Version')

$document.ready do
  App = Inesita::Application.new(
    router: Router,
    store: Store,
    layout: Layout,
    dispatcher: Dispatcher
  ).mount_to($document.body)
end

# dispatcher.rb
class Dispatcher
  include Inesita::Dispatcher

  def actions
    [:test] # list of all available actions
  end
end

# store.rb
class Store
  include Inesita::Store

  def initialize
    dispatcher.subscribe(:test) do |data|
      puts "got it #{data}"
    end
  end
end

# somewhere in component
button  onclick: -> { dispatcher.action(:test, hello: '123') } do
  text 'Click me!'
end

Any comments are welcomed

fazibear commented 8 years ago

I'll plan to refactor Inesita injections. Adding store or in this case action_dispatcher to apllication like this is not modular.

To make Inesita more modular I'll want to do something like this:

$document.ready do
  App = Inesita::Application.new(
    router: Router,
    layout: Layout,
    injections: {
        store: Store,
        dispatcher: Dispatcher
    }
  ).mount_to($document.body)
end

With this approach it possible to inject more than one store but also dispatcher or anything. You will be able to write a gem with dispatcher and use it.

fazibear commented 7 years ago

Hi, Inesita now supports injections so you can implement and inject dispatcher yourself.