execjs-rails 0.0.6.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7b3e545fec27d1e7906a4caa437b281dc5baa428
4
- data.tar.gz: ddc613d20daefde8cf884e39363b0a080d8619b8
3
+ metadata.gz: bd08829f22d695edb626c57dd61c2f92ffdbf3e4
4
+ data.tar.gz: d3a747916c9e43939397b58f7793cbcdf4075c66
5
5
  SHA512:
6
- metadata.gz: 818b63cf094c9f67d8c5a870d109b34c3d8c76c8daa4673b9109f56d85a22f41bda77cd0287200636136fbba8fcf61003a507f6511c85239c559f8f0fed66a24
7
- data.tar.gz: 267e16e2129de9fdb041af3c0a88d057081cd5d1abfc0afab4a7762c9d922865c6a7afe890b42e1661142cb8e2fa69c9ff04e0695043a489fbf9488e268a9cef
6
+ metadata.gz: 4e022025803f228d07f54d092e149fe95029de065bc71d10e1370c50d472a5c830ccec37020a3c052d593b1b7ce3051f11bb0e1ec85bdce4beb785f7eda5a00b
7
+ data.tar.gz: c2d089814b88cba3536d68c7e804fb1a1032ddfa34b337babbeb9cf76f265340a7c5c388c0b3a5e4bc091c00c3a79d88270c99ac26aca5c0c610d8d5d78e5a18
data/README.md CHANGED
@@ -3,22 +3,41 @@ execjs-rails
3
3
 
4
4
  # JavaScript-based rendering in Rails
5
5
 
6
- ## WARNING: Early release. There will be many changes
6
+ ## Why render via JavaScript in Rails?
7
7
 
8
- ## How to use it:
8
+ There are some great JavaScript UI frameworks out there now. Writing most of your view layer in a JavaScript-based framework cuts down on a lot of repetition (and potential points of divergence) as opposed to having Rails output HTML one way and JavaScript output HTML another. Rendering JavaScript-based view code on the server preserves SEO and, if you care and you're diligent, backward compatibility with older browsers.
9
9
 
10
- Include `execjs-rails` in your Gemfile.
10
+ ## Why ExecJS Rails?
11
11
 
12
- Create a file named `app/assets/javascripts/server.js` which contains the following:
12
+ ExecJS Rails has very few opinions. It's designed to get its opinions from your JavaScript code.
13
+
14
+ It was originally written out of some frustration with `react-rails`, which bakes in a lot of opinions we don't share. Should a server-side JavaScript context have a global `window` object? We thought "no", and `react-rails` at some point decided "yes" and broke some of our conditional checks. It also didn't have any hooks to override what it injects into the server-side JavaScript context. So we thought what would really be nice is to have an underpinning rendering library that doesn't care if you use React or something else and is largely opinion-free.
15
+
16
+ This view engine has one philosophy: Let JavaScript do it. Out of the box, it's configured to look for two JavaScript functions in your asset bundle: One to ask your JavaScript if it has a view for a given path, and another to actually return a rendered string. It's up to you to write JS code that will look up and render JS-based views. It's up to you how you want to build your JS bundle to accomplish that task. Or better yet, it's up to you to build a more opinionated framework on top of ExecJS Rails. Which is really what it's here for.
17
+
18
+ ## Quickstart Guide:
19
+
20
+ Include `execjs-rails` in your Gemfile, and create a file named `app/assets/javascripts/server.js` which contains the following:
13
21
  ```javascript
22
+ // This function will tell Rails whether or not ExecJS Rails has a view for this path.
23
+ function execjs_rails_has_view(path) {
24
+ // `path` looks like "users/show"
25
+ return false; // Do something smarter that will return `true` once in a while.
26
+ }
27
+
28
+ // This function will render a view for the given path.
14
29
  function execjs_rails_handler(path, opts) {
15
30
  // `path` looks like "users/show"
16
- // `opts` is a data object passed in from Rails
31
+ // `opts` is a data object passed in from Rails view assigns
32
+ return "Hello, World!"; // Do something smarter that returns HTML or something.
17
33
  }
18
34
  ```
35
+ Then you have:
36
+ * A Rails view helper, `execjs_render`, that will render output from your `execjs_rails_handler` JavaScript function.
37
+ * A Rails template handler that will do the same. You can write your controller code the exact same way as if you were using ERB. If no ERB view exists, it will use `execjs_rails_has_view` to determine whether or not ExecJS Rails can handle the view. If so, it will render via `execjs_rails_handler`.
38
+
39
+ That's pretty much it.
19
40
 
20
- Combine this approach with some fancy JavaScript UI frameworks and some smarty pants
21
- asset bundles, and you'll have shared client and server views for extra DRYness
22
- without sacrificing SEO.
41
+ ## Advanced Usage:
23
42
 
24
- Read the source for configuration options and more info.
43
+ For now, just read the source for configuration options and more info. You can override a lot of the default behavior.
@@ -1,3 +1,7 @@
1
+ function execjs_rails_has_view(path) {
2
+ return false;
3
+ }
4
+
1
5
  function execjs_rails_handler(path, opts) {
2
6
  var json = JSON.stringify(opts, null, " ");
3
7
  var code = "execjs_rails_handler('"+path+"', "+json+");"
@@ -6,6 +6,7 @@ module ExecJS
6
6
  config.execjs_rails.memoize_context = ::Rails.env.production?
7
7
  config.execjs_rails.on_error = lambda { |error| raise error }
8
8
  config.execjs_rails.server_javascripts = ['server.js']
9
+ config.execjs_rails.has_view_function_name = 'execjs_rails_has_view'
9
10
  config.execjs_rails.handler_function_name = 'execjs_rails_handler'
10
11
 
11
12
  config.execjs_rails.view_to_function_options = lambda do |view, options|
@@ -28,6 +28,10 @@ module ExecJS
28
28
  @@context.call(function, *args)
29
29
  end
30
30
 
31
+ def self.has_view(path)
32
+ @@context.call(@@config.has_view_function_name, path)
33
+ end
34
+
31
35
  def self.render(path, opts = {})
32
36
  output = self.call(@@config.handler_function_name, path, opts)
33
37
  fail "ExecJS call to render function `#{function}` returned null" if output.nil?
@@ -3,8 +3,8 @@ module ExecJS
3
3
  class TemplateResolver < ::ActionView::Resolver
4
4
 
5
5
  def find_templates(name, prefix, partial, details)
6
- if details[:formats].include?(:html)
7
- path = "#{prefix}/#{name}"
6
+ path = "#{prefix}/#{name}"
7
+ if details[:formats].include?(:html) && Renderer.has_view(path)
8
8
  [::ActionView::Template.new(
9
9
  "<%= execjs_render('#{path}') %>",
10
10
  "ExecJS - #{path}",
@@ -1,5 +1,5 @@
1
1
  module ExecJS
2
2
  module Rails
3
- VERSION = '0.0.6.1'
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: execjs-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Revelry Labs, LLC
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-25 00:00:00.000000000 Z
11
+ date: 2014-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: execjs