Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit 809c952

Browse files
committed
Move opal-jquery extension to separate file
1 parent 776b54e commit 809c952

File tree

5 files changed

+75
-74
lines changed

5 files changed

+75
-74
lines changed

config.ru

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ require 'bundler'
22
Bundler.require
33

44
require "opal/rspec"
5-
require "opal-jquery"
65

76
Opal::Config.arity_check_enabled = true
87

lib/hyper-react.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
require 'react/rendering_context'
2929
require 'react/state'
3030
require 'react/object'
31+
require "react/ext/opal-jquery/element"
3132
require 'reactive-ruby/isomorphic_helpers'
3233
require 'rails-helpers/top_level_rails_component'
3334
require 'reactive-ruby/version'
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Element.instance_eval do
2+
def self.find(selector)
3+
selector = begin
4+
selector.dom_node
5+
rescue
6+
selector
7+
end if `#{selector}.$dom_node !== undefined`
8+
`$(#{selector})`
9+
end
10+
11+
def self.[](selector)
12+
find(selector)
13+
end
14+
15+
define_method :render do |container = nil, params = {}, &block|
16+
if `#{self.to_n}._reactrb_component_class === undefined`
17+
`#{self.to_n}._reactrb_component_class = #{Class.new(React::Component::Base)}`
18+
end
19+
klass = `#{self.to_n}._reactrb_component_class`
20+
klass.class_eval do
21+
render(container, params, &block)
22+
end
23+
24+
React.render(React.create_element(`#{self.to_n}._reactrb_component_class`), self)
25+
end
26+
end if Object.const_defined?('Element')

lib/react/top_level.rb

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -103,30 +103,3 @@ def self.unmount_component_at_node(node)
103103
end
104104

105105
end
106-
107-
Element.instance_eval do
108-
def self.find(selector)
109-
selector = begin
110-
selector.dom_node
111-
rescue
112-
selector
113-
end if `#{selector}.$dom_node !== undefined`
114-
`$(#{selector})`
115-
end
116-
117-
def self.[](selector)
118-
find(selector)
119-
end
120-
121-
define_method :render do |container = nil, params = {}, &block|
122-
if `#{self.to_n}._reactrb_component_class === undefined`
123-
`#{self.to_n}._reactrb_component_class = #{Class.new(React::Component::Base)}`
124-
end
125-
klass = `#{self.to_n}._reactrb_component_class`
126-
klass.class_eval do
127-
render(container, params, &block)
128-
end
129-
130-
React.render(React.create_element(`#{self.to_n}._reactrb_component_class`), self)
131-
end
132-
end if Object.const_defined?('Element')
Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,68 @@
11
require 'spec_helper'
22

33
if opal?
4-
describe 'Element' do
5-
after(:each) do
6-
React::API.clear_component_class_cache
7-
end
4+
describe 'opal-jquery extensions' do
5+
describe 'Element' do
6+
after(:each) do
7+
React::API.clear_component_class_cache
8+
end
89

9-
it 'will reuse the wrapper componet class for the same Element' do
10-
stub_const 'Foo', Class.new(React::Component::Base)
11-
Foo.class_eval do
12-
param :name
13-
def render
14-
"hello #{params.name}"
15-
end
10+
it 'will reuse the wrapper componet class for the same Element' do
11+
stub_const 'Foo', Class.new(React::Component::Base)
12+
Foo.class_eval do
13+
param :name
14+
def render
15+
"hello #{params.name}"
16+
end
1617

17-
def component_will_unmount
18+
def component_will_unmount
1819

20+
end
1921
end
20-
end
2122

22-
expect_any_instance_of(Foo).to_not receive(:component_will_unmount)
23+
expect_any_instance_of(Foo).to_not receive(:component_will_unmount)
2324

24-
test_div = Element.new(:div)
25-
test_div.render { Foo(name: 'fred') }
26-
test_div.render { Foo(name: 'freddy') }
27-
expect(Element[test_div].find('span').html).to eq('hello freddy')
28-
end
25+
test_div = Element.new(:div)
26+
test_div.render { Foo(name: 'fred') }
27+
test_div.render { Foo(name: 'freddy') }
28+
expect(Element[test_div].find('span').html).to eq('hello freddy')
29+
end
2930

30-
it 'renders a top level component using render with a block' do
31-
stub_const 'Foo', Class.new(React::Component::Base)
32-
Foo.class_eval do
33-
param :name
34-
def render
35-
"hello #{params.name}"
31+
it 'renders a top level component using render with a block' do
32+
stub_const 'Foo', Class.new(React::Component::Base)
33+
Foo.class_eval do
34+
param :name
35+
def render
36+
"hello #{params.name}"
37+
end
3638
end
39+
test_div = Element.new(:div)
40+
test_div.render { Foo(name: 'fred') }
41+
expect(Element[test_div].find('span').html).to eq('hello fred')
3742
end
38-
test_div = Element.new(:div)
39-
test_div.render { Foo(name: 'fred') }
40-
expect(Element[test_div].find('span').html).to eq('hello fred')
41-
end
4243

43-
it 'renders a top level component using render with a container and params ' do
44-
test_div = Element.new(:div)
45-
test_div.render(:span, id: :render_test_span) { 'hello' }
46-
expect(Element[test_div].find('#render_test_span').html).to eq('hello')
47-
end
44+
it 'renders a top level component using render with a container and params ' do
45+
test_div = Element.new(:div)
46+
test_div.render(:span, id: :render_test_span) { 'hello' }
47+
expect(Element[test_div].find('#render_test_span').html).to eq('hello')
48+
end
4849

49-
it 'will find the DOM node given a react element' do
50-
stub_const 'Foo', Class.new(React::Component::Base)
51-
Foo.class_eval do
52-
def render
53-
div { 'hello' }
50+
it 'will find the DOM node given a react element' do
51+
stub_const 'Foo', Class.new(React::Component::Base)
52+
Foo.class_eval do
53+
def render
54+
div { 'hello' }
55+
end
5456
end
55-
end
5657

57-
expect(Element[renderToDocument(Foo)].html).to eq('hello')
58-
end
58+
expect(Element[renderToDocument(Foo)].html).to eq('hello')
59+
end
5960

60-
it "accepts plain js object as selector" do
61-
expect {
62-
Element[`window`]
63-
}.not_to raise_error
61+
it "accepts plain js object as selector" do
62+
expect {
63+
Element[`window`]
64+
}.not_to raise_error
65+
end
6466
end
6567
end
6668
end

0 commit comments

Comments
 (0)