This repository was archived by the owner on Oct 19, 2018. It is now read-only.
This repository was archived by the owner on Oct 19, 2018. It is now read-only.
need way to dynamically mount components (i.e. after ajax calls) #252
Open
Description
require 'jquery'
require 'opal-jquery'
module Hyperloop
def self.mount_all
Element['[data-hyperloop-mount]'].each do |mount_point|
component_name = mount_point.attr('data-hyperloop-mount')
component = nil
begin
component = Object.const_get(component_name)
rescue
message = "Could not find Component class named #{component_name}"
`console.error(message)`
next
end
params = Hash[*mount_point.data.collect do |name, value|
[name.underscore, value] unless name == 'hyperloopMount'
end.compact.flatten(1)]
React.render(React.create_element(component, params), mount_point)
end
end
%x{
window.hyperloop_mount_all = function() {
#{Hyperloop.mount_all}
}
}
end
From opal you can call Hyperloop.mount_all
or from js land its hyperloop_mount_all()
It will find all components with the data-hyperloop-mount data attribute, and the name of the component will be the attribute value. For example
<DIV data-hyperloop-mount='MyBigComponent' data-param1='some data'>
</DIV>
will mount MyBigComponent at the DIV and pass it 'some data' as param1
Metadata
Metadata
Assignees
Labels
No labels