-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.rb
79 lines (70 loc) · 1.88 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
module Ovto
class App
# Create an App and start it
def self.run(*args)
new.run(*args)
end
def initialize
@state = self.class.const_get('State').new
@wired_actions = nil
end
attr_reader :state
def actions
@wired_actions
end
# Internal use only
def _set_state(new_state)
@state = new_state
end
# Start this app
def run(*args)
Ovto.log_error{ _run(*args) }
end
# Called when this app is started
def setup
# override this if needed
end
private
# Setup runtime and wired_actions
def _run(id: nil)
runtime = Ovto::Runtime.new(self)
actions = self.class.const_get('Actions').new
@wired_actions = WiredActions.new(actions, self, runtime)
actions.wired_actions = @wired_actions
view = create_view
if id
%x{
document.addEventListener('DOMContentLoaded', function(){
var container = document.getElementById(id);
if (!container) {
throw "Ovto::App#run: tag with id='" + id + "' was not found";
}
#{start_application(runtime, view, `container`)}
});
}
else
start_application(runtime, view, nil)
end
end
# Instantiate MyApp::MainComponent
def create_view
begin
main_component_class = self.class.const_get('MainComponent')
rescue NameError => orig_ex
begin
self.class.const_get('View')
rescue NameError
raise orig_ex
else
raise "Since Ovto 0.3.0, View is renamed to MainComponent. Please rename "+
"#{self.class}::View to #{self.class}::MainComponent"
end
end
return main_component_class.new(@wired_actions)
end
def start_application(runtime, view, container)
runtime.run(view, container)
setup
end
end
end