Skip to content

Commit 6ea19d1

Browse files
author
John Lynch
committed
Implement unobtrusive rendering for React components.
1 parent 8bb7697 commit 6ea19d1

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

lib/react/rails/view_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ def react_component(name, args = {}, options = {})
3535
result << react_javascript_tag(name, html_options[:id], args)
3636
end
3737

38+
def react_component_ujs(name, args = {}, options = {})
39+
content_tag(:div, nil, {:data => {:react => name}.merge(args)})
40+
end
41+
3842
private
3943
# Returns +[html_tag, html_options]+.
4044
def react_parse_options(options)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
(function($, undefined) {
2+
3+
/**
4+
* Unobtrusive scripting adapter for React
5+
*
6+
* Requires jQuery 1.7.0 or later.
7+
*
8+
* Released under the MIT license
9+
*
10+
*/
11+
12+
// Cut down on the number of issues from people inadvertently including react_ujs twice
13+
// by detecting and raising an error when it happens.
14+
if ( $.react !== undefined ) {
15+
$.error('react-ujs has already been loaded!');
16+
}
17+
18+
// Turbolinks uses page:load
19+
$(document).on('ready page:load', function () {
20+
$("[data-react]").each(function() {
21+
var args = $(this).data();
22+
var componentName = args['react'];
23+
delete args['react'];
24+
React.renderComponent(window[componentName](args), this)
25+
})
26+
})
27+
28+
})( jQuery );

0 commit comments

Comments
 (0)