Class: Page

Inherits:
Object show all
Defined in:
lib/volt/templates/page.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePage

Returns a new instance of Page.



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
# File 'lib/volt/templates/page.rb', line 31

def initialize

  # debugger
  puts "------ Page Loaded -------"
  @model_classes = {}
  
  # Run the code to setup the page
  @page = ReactiveValue.new(Model.new)#({}, nil, 'page', @model_classes))
  @store = ReactiveValue.new(Model.new)#({}, nil, 'store', @model_classes))
  
  @url = ReactiveValue.new(URL.new)
  @params = @url.params
  @url_tracker = UrlTracker.new(self)

  @events = DocumentEvents.new
  @render_queue = RenderQueue.new
  
  # Add event for link clicks to handle all a onclick
  # EventBinding.new(self, )
  
  # Setup escape binding for console
  %x{
    $(document).keyup(function(e) {
      if (e.keyCode == 27) {
        Opal.gvars.page.$launch_console();
      }
    });
    
    $(document).on('click', 'a', function(event) {        
      Opal.gvars.page.$link_clicked($(this).attr('href'));
      event.stopPropagation();
      
      return false;
    });
  }

  channel.on('message') do |message|
    puts "GOT: #{message}"
  end
end

Instance Attribute Details

#pageObject (readonly)

Returns the value of attribute page.



29
30
31
# File 'lib/volt/templates/page.rb', line 29

def page
  @page
end

#paramsObject (readonly)

Returns the value of attribute params.



29
30
31
# File 'lib/volt/templates/page.rb', line 29

def params
  @params
end

#render_queueObject (readonly)

Returns the value of attribute render_queue.



29
30
31
# File 'lib/volt/templates/page.rb', line 29

def render_queue
  @render_queue
end

#routesObject (readonly)

Returns the value of attribute routes.



29
30
31
# File 'lib/volt/templates/page.rb', line 29

def routes
  @routes
end

#storeObject (readonly)

Returns the value of attribute store.



29
30
31
# File 'lib/volt/templates/page.rb', line 29

def store
  @store
end

#templatesObject (readonly)

Returns the value of attribute templates.



29
30
31
# File 'lib/volt/templates/page.rb', line 29

def templates
  @templates
end

#urlObject (readonly)

Returns the value of attribute url.



29
30
31
# File 'lib/volt/templates/page.rb', line 29

def url
  @url
end

Instance Method Details

#add_model(model_name) ⇒ Object



99
100
101
102
103
# File 'lib/volt/templates/page.rb', line 99

def add_model(model_name)
  # puts "ADD MODEL: #{model_name.inspect} - #{model_name.camelize.inspect}"
  
  @model_classes[["*", "_#{model_name}"]] = Object.const_get(model_name.camelize)
end

#add_routes(&block) ⇒ Object



112
113
114
115
# File 'lib/volt/templates/page.rb', line 112

def add_routes(&block)
  @routes = Routes.new.define(&block)
  @url.cur.router = @routes
end

#add_template(name, template, bindings) ⇒ Object



105
106
107
108
109
110
# File 'lib/volt/templates/page.rb', line 105

def add_template(name, template, bindings)
  # puts "Add Template: #{name}\n#{template.inspect}\n#{bindings.inspect}"
  @templates ||= {}
  @templates[name] = {'html' => template, 'bindings' => bindings}
  # puts "Add Template: #{name}"
end

#binding_nameObject

We provide a binding_name, so we can bind events on the document



83
84
85
# File 'lib/volt/templates/page.rb', line 83

def binding_name
  'page'
end

#channelObject



91
92
93
# File 'lib/volt/templates/page.rb', line 91

def channel
  @channel ||= Channel.new
end

#eventsObject



95
96
97
# File 'lib/volt/templates/page.rb', line 95

def events
  @events
end

#launch_consoleObject



87
88
89
# File 'lib/volt/templates/page.rb', line 87

def launch_console
  puts "Launch Console"
end


72
73
74
75
76
77
78
79
80
# File 'lib/volt/templates/page.rb', line 72

def link_clicked(url)
  # Skip when href == ''
  return if url.blank?

  # Normalize url
  # Benchmark.bm(1) do
    @url.parse("http://localhost:3000" + url)
  # end
end

#startObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/volt/templates/page.rb', line 117

def start
  # Setup to render template
  Element.find('body').html = "<!-- $CONTENT --><!-- $/CONTENT -->"

  main_controller = IndexController.new

  # Setup main page template
  TemplateRenderer.new(DomTarget.new, main_controller, 'CONTENT', 'home/index/index/body')

  # Setup title listener template
  title_target = AttributeTarget.new
  title_target.on('changed') do
    title = title_target.to_html
    `document.title = title;`
  end
  TemplateRenderer.new(title_target, main_controller, "main", "home/index/index/title")
  
  @url_tracker.url_updated(true)
end