Module: Fron::Behaviors::Routes
- Defined in:
- opal/fron/core/behaviors/routes.rb
Overview
Behavior for handling routes in applications.
Class Method Summary collapse
-
.handle_hash_change(hash) ⇒ Object
Handles hash change event.
-
.included(base) ⇒ Object
Runs for included classes.
-
.listen ⇒ Object
Listen on events (maily for tests).
-
.register(path, action, component) ⇒ Object
Register a route.
-
.route(item) ⇒ Object
Registers routes from the registry.
Class Method Details
.handle_hash_change(hash) ⇒ Object
Handles hash change event
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
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 |
.listen ⇒ Object
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
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 |