Class: Volt::TemplateBinding
- Inherits:
-
BaseBinding
- Object
- BaseBinding
- Volt::TemplateBinding
- Defined in:
- lib/volt/page/bindings/template_binding.rb
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from BaseBinding
#binding_name, #context, #target
Instance Method Summary collapse
- #call_ready ⇒ Object
-
#check_for_template?(path) ⇒ Boolean
Returns true if there is a template at the path.
- #clear_grouped_controller ⇒ Object
-
#initialize(page, target, context, binding_name, binding_in_path, getter) ⇒ TemplateBinding
constructor
A new instance of TemplateBinding.
-
#path_for_template(lookup_path, force_section = nil) ⇒ Object
Takes in a lookup path and returns the full path for the matching template.
-
#queue_clear_grouped_controller ⇒ Object
On the next tick, we clear the grouped controller so that any changes to template paths will create a new controller and trigger the action.
- #remove ⇒ Object
-
#render_template(full_path, controller_path) ⇒ Object
The context for templates can be either a controller, or the original context.
- #setup_path(binding_in_path) ⇒ Object
- #update(path, section_or_arguments = nil, options = {}) ⇒ Object
Methods inherited from BaseBinding
Constructor Details
#initialize(page, target, context, binding_name, binding_in_path, getter) ⇒ TemplateBinding
Returns a new instance of TemplateBinding.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/volt/page/bindings/template_binding.rb', line 7 def initialize(page, target, context, binding_name, binding_in_path, getter) super(page, target, context, binding_name) # Binding in path is the path for the template this binding is in setup_path(binding_in_path) @current_template = nil # Run the initial render @computation = -> do # Don't try to render if this has been removed if @context # Render update(*@context.instance_eval(&getter)) end end.watch! end |
Instance Method Details
#call_ready ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/volt/page/bindings/template_binding.rb', line 200 def call_ready if @controller # Set the current section on the controller if it wants so it can manipulate # the dom if needed if @controller.respond_to?(:section=) @controller.section = @current_template.dom_section end controller_send(:"#{@action}_ready") if @action end end |
#check_for_template?(path) ⇒ Boolean
Returns true if there is a template at the path
33 34 35 |
# File 'lib/volt/page/bindings/template_binding.rb', line 33 def check_for_template?(path) @page.templates[path] end |
#clear_grouped_controller ⇒ Object
155 156 157 158 159 160 |
# File 'lib/volt/page/bindings/template_binding.rb', line 155 def clear_grouped_controller if @grouped_controller @grouped_controller.clear @grouped_controller = nil end end |
#path_for_template(lookup_path, force_section = nil) ⇒ Object
Takes in a lookup path and returns the full path for the matching template. Also returns the controller and action name if applicable.
Looking up a path is fairly simple. There are 4 parts needed to find the html to be rendered. File paths look like this: app/component/views/controller_name/view.html Within the html file may be one or more sections.
-
component (app/comp)
-
controller
-
view
-
sections
When searching for a file, the lookup starts at the section, and moves up. when moving up, default values are provided for the section, then view/section, etc.. until a file is either found or the component level is reached.
The defaults are as follows:
-
component - main
-
controller - main
-
view - main
-
section - body
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/volt/page/bindings/template_binding.rb', line 58 def path_for_template(lookup_path, force_section = nil) parts = lookup_path.split('/') parts_size = parts.size default_parts = %w(main main index body) # When forcing a sub template, we can default the sub template section default_parts[-1] = force_section if force_section (5 - parts_size).times do |path_position| # If they passed in a force_section, we can skip the first next if force_section && path_position == 0 full_path = [@collection_name, @controller_name, @page_name, nil] start_at = full_path.size - parts_size - path_position full_path.size.times do |index| if index >= start_at if (part = parts[index - start_at]) full_path[index] = part else full_path[index] = default_parts[index] end end end path = full_path.join('/') if check_for_template?(path) controller = nil # Don't return a controller if we are just getting another section # from the same controller if path_position >= 1 # Lookup the controller controller = [full_path[0], full_path[1] + '_controller', full_path[2]] end return path, controller end end [nil, nil] end |
#queue_clear_grouped_controller ⇒ Object
On the next tick, we clear the grouped controller so that any changes to template paths will create a new controller and trigger the action.
142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/volt/page/bindings/template_binding.rb', line 142 def queue_clear_grouped_controller if Volt.in_browser? # In the browser, we want to keep a grouped controller around during a single run # of the event loop. To make that happen, we clear it on the next tick. `setImmediate(function() {` clear_grouped_controller `});` else # For the backend, clear it immediately clear_grouped_controller end end |
#remove ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/volt/page/bindings/template_binding.rb', line 212 def remove clear_grouped_controller if @current_template # Remove the template if one has been rendered, when the template binding is # removed. @current_template.remove end super if @controller controller_send(:"#{@action}_removed") if @action @controller = nil end end |
#render_template(full_path, controller_path) ⇒ Object
The context for templates can be either a controller, or the original context.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/volt/page/bindings/template_binding.rb', line 163 def render_template(full_path, controller_path) args = @arguments ? [SubContext.new(@arguments, nil, true)] : [] @controller = nil # Fetch grouped controllers if we're grouping @controller = @grouped_controller.get if @grouped_controller # The action to be called and rendered @action = nil if @controller # Track that we're using the group controller @grouped_controller.inc if @grouped_controller else # Otherwise, make a new controller controller_class, @action = get_controller(controller_path) if controller_class # Setup the controller @controller = controller_class.new(*args) else @controller = ModelController.new(*args) end # Trigger the action controller_send(@action) if @action # Track the grouped controller @grouped_controller.set(@controller) if @grouped_controller end @current_template = TemplateRenderer.new(@page, @target, @controller, @binding_name, full_path) call_ready end |
#setup_path(binding_in_path) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/volt/page/bindings/template_binding.rb', line 25 def setup_path(binding_in_path) path_parts = binding_in_path.split('/') @collection_name = path_parts[0] @controller_name = path_parts[1] @page_name = path_parts[2] end |
#update(path, section_or_arguments = nil, options = {}) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/volt/page/bindings/template_binding.rb', line 102 def update(path, section_or_arguments = nil, = {}) Computation.run_without_tracking do # Remove existing template and call _removed controller_send(:"#{@action}_removed") if @action && @controller @current_template.remove if @current_template @options = # A blank path needs to load a missing template, otherwise it tries to load # the same template. path = path.blank? ? '---missing---' : path section = nil @arguments = nil if section_or_arguments.is_a?(String) # Render this as a section section = section_or_arguments else # Use the value passed in as the default arguments @arguments = section_or_arguments end # Sometimes we want multiple template bindings to share the same controller (usually # when displaying a :Title and a :Body), this instance tracks those. if @options && (controller_group = @options[:controller_group]) @grouped_controller = GroupedControllers.new(controller_group) else clear_grouped_controller end full_path, controller_path = path_for_template(path, section) render_template(full_path, controller_path) queue_clear_grouped_controller end end |