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
71
72
# 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
  # channel.on('message') do |message|
  #   # puts "GOT: #{message}"
  #   # `console.log('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



102
103
104
105
106
# File 'lib/volt/templates/page.rb', line 102

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



115
116
117
118
# File 'lib/volt/templates/page.rb', line 115

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

#add_template(name, template, bindings) ⇒ Object



108
109
110
111
112
113
# File 'lib/volt/templates/page.rb', line 108

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



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

def binding_name
  'page'
end

#channelObject



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

def channel
  @channel ||= Channel.new
end

#eventsObject



98
99
100
# File 'lib/volt/templates/page.rb', line 98

def events
  @events
end

#launch_consoleObject



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

def launch_console
  puts "Launch Console"
end


74
75
76
77
78
79
80
81
82
83
# File 'lib/volt/templates/page.rb', line 74

def link_clicked(url)
  puts "Link Clicked to: #{url}"
  # Skip when href == ''
  return if url.blank?

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

#startObject



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

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