lib/motion-kit/helpers/tree_layout.rb in motion-kit-0.14.2 vs lib/motion-kit/helpers/tree_layout.rb in motion-kit-0.15.0

- old
+ new

@@ -58,10 +58,11 @@ end def initialize(args={}) super @child_layouts = [] + @reapply_blocks = [] @elements = {} end # The main view. This method builds the layout and returns the root view. def view @@ -136,17 +137,12 @@ # part of this layout. The views in a child layout are not styled, but # those layouts will receive a `reapply!` message if no root is specified. def reapply! root ||= self.view @layout_state = :reapply + run_reapply_blocks - @elements.each do |element_id, elements| - elements.each do |element| - style_and_context(element, element_id) - end - end - @child_layouts.each do |child_layout| child_layout.reapply! end @layout_state = :initial @@ -156,25 +152,53 @@ def reapply? @layout_state == :reapply end - # Calls the style method of all objects in the view hierarchy + # Only intended for private use + def reapply_blocks + @reapply_blocks ||= [] + end + + # Blocks passed to `reapply` are only run when `reapply!` is called. def reapply(&block) raise ArgumentError.new('Block required') unless block + raise InvalidDeferredError.new('reapply must be run inside of a context') unless @context if reapply? yield end + + block = block.weak! + parent_layout.reapply_blocks << [@context, block] return self end + # Only intended for private use + def run_reapply_blocks + self.reapply_blocks.each do |target, block| + context(target, &block) + end + end + def initial? @layout_state == :initial end + def always(&block) + raise ArgumentError.new('Block required') unless block + + if initial? + yield + end + reapply(&block) + + return self + end + def initial(&block) raise ArgumentError.new('Block required') unless block + puts('this method is no longer necessary! all code that *isn\'t in a `reapply` block is now only applied during initial setup.') if initial? yield end return self