Module: Fron::Behaviors::Routes

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

Overview

Components

Class Method Summary collapse

Class Method Details

.handleHashChange(hash) ⇒ Object

Handles hash change event

Parameters:


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

def self.handleHashChange(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


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

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

  return if @initialized
  DOM::Window.on('popstate') { handleHashChange DOM::Window.state }
  @routes = []
  @initialized = true
end

.register(path, action, component) ⇒ Object

Register a route

Parameters:


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

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

.route(registry) ⇒ Object

Registers routes from the registry

Parameters:

  • registry (Array)

    The routes


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

def self.route(registry)
  registry.each do |item|
    path, action = item
    fail "There is no method #{action} on #{self}" unless respond_to? action
    Routes.register path, action, self
  end
end