Method: Bluesky::PureComponent.install_hooks

Defined in:
lib/bluesky/pure_component.rb

.install_hooks(debug = false) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/bluesky/pure_component.rb', line 87

def self.install_hooks(debug=false)

  @descendants.each do |subclass|

    if subclass.instance_methods.include?(:component_did_mount) ||
       subclass.instance_methods.include?(:component_will_unmount)
      subclass.class_eval { `Opal.defn(self, '$$mountable', true);` }
    end

    if subclass.instance_methods.include?(:component_will_mount)
      subclass.class_eval { `Opal.defn(self, '$$hook_will_mount', true);` }
    end
    subclass.send(:alias_method, :do_render, :render) unless
      subclass.instance_methods.include?(:do_render)

    subclass.send(:define_method, :render) do
      begin
        $$.console.time("#{subclass.name}:render") if debug
        %x{
          var contents = #{do_render};
          if (self.$$mountable) contents.properties.ref = self;
          return contents;
        }
      rescue Object => err
        warn err
        div({ class: 'broken', style: { display: :none } }, [err.message])
      ensure
        $$.console.timeEnd("#{subclass.name}:render") if debug
      end
    end

  end

end