js_render 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +24 -2
- data/lib/js_render.rb +1 -0
- data/lib/js_render/configuration.rb +2 -0
- data/lib/js_render/renderer.rb +12 -1
- data/lib/js_render/utils.rb +6 -0
- data/lib/js_render/utils/camelize.rb +9 -0
- data/lib/js_render/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3efa8f3b5071ab1391bc0f0884c7125ddfc875bc
|
4
|
+
data.tar.gz: eddc754107e5f99f70b9cb48855781023379735b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f1a34fc4f3b80733f4b3b5335902a1ed43b40db6442cbc73e32f78b847bb263fdfaa9928dfcd5fa671910c95fb3340b6145b08039a0758170c8b969dd8426f84
|
7
|
+
data.tar.gz: 5b4011ffccaae946dfac1903abee032074e0cda6392b27e6fcd5af4c73675d55c88de7ebac4a96718e9ef102fbdb1b692972a55b58adb0727e041324f4ec3c5f
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
|
4
4
|
# JsRender
|
5
5
|
|
6
|
-
JsRender is an unopinionated Ruby library for rendering JavaScript "components" on the server side. This approach works with [React](https://facebook.github.io/react/), [Angular](
|
6
|
+
JsRender is an unopinionated Ruby library for rendering JavaScript "components" on the server side. This approach works with [React](https://facebook.github.io/react/), [Angular](https://angular.io/), [Ember](http://emberjs.com/), or any other library of your choice. The only requirement is that there is a JavaScript function that returns HTML for the component or view such that it can be properly rendered on the server side (e.g. [`ReactDOMServer.renderToString`](https://facebook.github.io/react/docs/top-level-api.html#reactdomserver.rendertostring) in React).
|
7
7
|
|
8
8
|
The library works in two essential parts:
|
9
9
|
- Calls a JavaScript function (defined by the user) that returns HTML. This allows us to render our JS component/view when the page initially loads, rather than having the delay of doing it solely on the client side.
|
@@ -150,7 +150,29 @@ Methods you can override if you subclass `JsRender::AssetFinder::Base`:
|
|
150
150
|
|
151
151
|
> Defaults to `nil`
|
152
152
|
|
153
|
-
**
|
153
|
+
**key_transforms**
|
154
|
+
|
155
|
+
An array of lambdas (or singletons that implement a `call` method) that can transform the keys of the data being passed into the component. The lambdas in the array are called in order, and the result of a lambda is passed to the next transform (as a string) for each key.
|
156
|
+
|
157
|
+
`JsRender::Utils::Camelize` is a provided utility that will transform keys to camel case.
|
158
|
+
|
159
|
+
Note: This only works if the component data is passed in as a hash, if it is already passed in as JSON, then the transforms will be ignored.
|
160
|
+
|
161
|
+
Example:
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
JsRender.config.key_transforms = [
|
165
|
+
JsRender::Utils::Camelize,
|
166
|
+
-> (key) { key + '_SomeEnding'}
|
167
|
+
]
|
168
|
+
|
169
|
+
# data => { camel_case: 2, something_else: 3 }
|
170
|
+
# would be transformed to => { camelCase_SomeEnding: 2, somethingElse_SomeEnding: 3 }
|
171
|
+
```
|
172
|
+
|
173
|
+
> Defaults to `[]`
|
174
|
+
|
175
|
+
**should_server_render**
|
154
176
|
|
155
177
|
This config option is a boolean that specifies if the server render function and associated JS should be executed and run. When it is false, it only returns the span with the unique ID that the client side render function relies upon. This is meant for development purposes and enables things like console logging etc. that would normally cause errors in the `ExecJS` runtime.
|
156
178
|
|
data/lib/js_render.rb
CHANGED
@@ -23,6 +23,7 @@ module JsRender
|
|
23
23
|
:client_render_function,
|
24
24
|
:use_asset_pipeline,
|
25
25
|
:asset_finder_class,
|
26
|
+
:key_transforms,
|
26
27
|
:should_server_render
|
27
28
|
|
28
29
|
def initialize
|
@@ -33,6 +34,7 @@ module JsRender
|
|
33
34
|
@client_render_function = 'window.render*Client'
|
34
35
|
@use_asset_pipeline = false
|
35
36
|
@asset_finder_class = nil
|
37
|
+
@key_transforms = []
|
36
38
|
@should_server_render = true
|
37
39
|
end
|
38
40
|
end
|
data/lib/js_render/renderer.rb
CHANGED
@@ -13,7 +13,10 @@ module JsRender
|
|
13
13
|
|
14
14
|
def initialize(component_name, data)
|
15
15
|
@component_name = component_name
|
16
|
-
|
16
|
+
unless data.is_a?(String)
|
17
|
+
transform_keys(data)
|
18
|
+
data = data.to_json
|
19
|
+
end
|
17
20
|
@json_data = data
|
18
21
|
@uuid = SecureRandom.uuid
|
19
22
|
end
|
@@ -64,5 +67,13 @@ module JsRender
|
|
64
67
|
AssetFinder::Base.new
|
65
68
|
end
|
66
69
|
end
|
70
|
+
|
71
|
+
def transform_keys(data)
|
72
|
+
JsRender.config.key_transforms.reduce(data) do |transformed_data, transform|
|
73
|
+
transformed_data.tap do |d|
|
74
|
+
d.keys.each { |k| d[transform.call(k.to_s)] = d.delete(k) }
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
67
78
|
end
|
68
79
|
end
|
data/lib/js_render/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: js_render
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Lehman
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-09
|
11
|
+
date: 2016-10-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -219,6 +219,8 @@ files:
|
|
219
219
|
- lib/js_render/rails/railtie.rb
|
220
220
|
- lib/js_render/rails/view_helpers.rb
|
221
221
|
- lib/js_render/renderer.rb
|
222
|
+
- lib/js_render/utils.rb
|
223
|
+
- lib/js_render/utils/camelize.rb
|
222
224
|
- lib/js_render/version.rb
|
223
225
|
homepage: http://github.com/jdlehman/js_render
|
224
226
|
licenses:
|