Module: Fron::Behaviors::Routes

Defined in:
opal/fron/core/behaviors/routes.rb

Overview

Behavior for handling routes in applications.

Class Method Summary collapse

Class Method Details

.handle_hash_change(hash) ⇒ Object

Handles hash change event

Parameters:



21
22
23
24
25
26
27
# File 'opal/fron/core/behaviors/routes.rb', line 21

def self.handle_hash_change(hash)
  routes = @routes.select { |route_| route_[:path] =~ hash }
  routes.each do |route|
    matches = hash.match route[:path]
    route[:component].send route[:action], *matches[1..-1]
  end
end

.included(base) ⇒ Object

Runs for included classes

Parameters:

  • base (Class)

    The class



32
33
34
35
36
37
38
# File 'opal/fron/core/behaviors/routes.rb', line 32

def self.included(base)
  base.register self, [:route]

  return if @initialized
  @initialized = true
  @routes = []
end

.listenObject

Listen on events (maily for tests)



41
42
43
# File 'opal/fron/core/behaviors/routes.rb', line 41

def self.listen
  DOM::Window.on('popstate') { handle_hash_change DOM::Window.state }
end

.register(path, action, component) ⇒ Object

Register a route

Parameters:



10
11
12
13
14
15
16
# File 'opal/fron/core/behaviors/routes.rb', line 10

def self.register(path, action, component)
  @routes << {
    path: Regexp.new(path),
    action: action,
    component: component
  }
end

.route(item) ⇒ Object

Registers routes from the registry

Parameters:

  • item (Array)

    The route



48
49
50
51
52
# File 'opal/fron/core/behaviors/routes.rb', line 48

def self.route(item)
  path, action = item[:args]
  raise "There is no method #{action} on #{self}" unless respond_to? action
  Routes.register path, action, self
end