ember-rails-lite 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +19 -0
- data/README.md +114 -0
- data/lib/ember-rails-lite.rb +1 -0
- data/lib/ember/filters/haml.rb +11 -0
- data/lib/ember/filters/slim.rb +5 -0
- data/lib/ember/handlebars/assets/ember-precompiler.js +28 -0
- data/lib/ember/handlebars/source.rb +54 -0
- data/lib/ember/handlebars/template.rb +60 -0
- data/lib/ember/handlebars/version.rb +7 -0
- data/lib/ember/rails/engine.rb +22 -0
- data/lib/ember/rails/version.rb +5 -0
- data/lib/ember/version.rb +5 -0
- data/lib/ember_rails.rb +56 -0
- data/lib/generators/ember/bootstrap_generator.rb +56 -0
- data/lib/generators/ember/controller_generator.rb +24 -0
- data/lib/generators/ember/generator_helpers.rb +19 -0
- data/lib/generators/ember/install_generator.rb +94 -0
- data/lib/generators/ember/model_generator.rb +44 -0
- data/lib/generators/ember/resource_override.rb +32 -0
- data/lib/generators/ember/view_generator.rb +20 -0
- data/lib/generators/templates/app.js +10 -0
- data/lib/generators/templates/application.handlebars +3 -0
- data/lib/generators/templates/array_controller.js +3 -0
- data/lib/generators/templates/controller.js +3 -0
- data/lib/generators/templates/model.js +5 -0
- data/lib/generators/templates/object_controller.js +3 -0
- data/lib/generators/templates/router.js +17 -0
- data/lib/generators/templates/store.js +5 -0
- data/lib/generators/templates/view.handlebars +5 -0
- data/lib/generators/templates/view.js +3 -0
- data/vendor/ember/development/ember-data.js +4176 -0
- data/vendor/ember/development/ember.js +23164 -0
- data/vendor/ember/development/handlebars-runtime.js +229 -0
- data/vendor/ember/development/handlebars.js +1895 -0
- data/vendor/ember/production/ember-data.js +4168 -0
- data/vendor/ember/production/ember.js +23048 -0
- data/vendor/ember/production/handlebars-runtime.js +2 -0
- data/vendor/ember/production/handlebars.js +2 -0
- data/vendor/ember/spade/ember-data.js +1 -0
- data/vendor/ember/spade/ember.js +1 -0
- metadata +185 -0
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2012 Keith Pitt, Rob Monie, Joao Carlos, Paul Chavard and ember-rails contributors
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
7
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
8
|
+
so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
# ember-rails [![Build Status](https://secure.travis-ci.org/emberjs/ember-rails.png?branch=master)](http://travis-ci.org/emberjs/ember-rails) [![Dependency Status](https://gemnasium.com/emberjs/ember-rails.png)](https://gemnasium.com/emberjs/ember-rails)
|
2
|
+
|
3
|
+
ember-rails allows you to include [Ember.JS](http://emberjs.com/) into your Rails 3.1+ application.
|
4
|
+
|
5
|
+
The gem will also pre-compile your handlebars templates when building your asset pipeline. It includes development and production copies of Ember.
|
6
|
+
|
7
|
+
You can see an example of how to use the gem [here](https://github.com/keithpitt/ember-rails-example). There is also a great tutorial by [Dan Gebhardt](https://twitter.com/#!/dgeb) called "[Beginning Ember.js on Rails](http://www.cerebris.com/blog/2012/01/24/beginning-ember-js-on-rails-part-1/)" which is a great read if your just starting out with Rails and Ember.js
|
8
|
+
|
9
|
+
## Getting started
|
10
|
+
|
11
|
+
Add the gem to your application Gemfile:
|
12
|
+
|
13
|
+
gem "ember-rails"
|
14
|
+
|
15
|
+
Run `bundle install` and add the following line to `app/assets/javascripts/application.js`:
|
16
|
+
|
17
|
+
//= require ember
|
18
|
+
|
19
|
+
Ember-rails will use the production build of Ember.js when Rails is running in
|
20
|
+
production mode, and the development build otherwise.
|
21
|
+
|
22
|
+
## Architecture
|
23
|
+
|
24
|
+
Ember does not require an organized file structure. However, ember-rails allows you
|
25
|
+
to use `rails g ember:bootstrap` to create the following directory structure under `app/assets/javascripts`:
|
26
|
+
|
27
|
+
controllers/
|
28
|
+
helpers/
|
29
|
+
models/
|
30
|
+
routes/
|
31
|
+
templates/
|
32
|
+
views/
|
33
|
+
|
34
|
+
Additionally, it will add the following lines to `app/assets/javascripts/application.js`.
|
35
|
+
By default, it uses the Rails Application's name and creates an `rails_app_name.js`
|
36
|
+
file to setup application namespace and initial requires:
|
37
|
+
|
38
|
+
//= require ember
|
39
|
+
//= require ember/app
|
40
|
+
|
41
|
+
*Example:*
|
42
|
+
|
43
|
+
rails g ember:bootstrap
|
44
|
+
insert app/assets/javascripts/application.js
|
45
|
+
create app/assets/javascripts/models
|
46
|
+
create app/assets/javascripts/models/.gitkeep
|
47
|
+
create app/assets/javascripts/controllers
|
48
|
+
create app/assets/javascripts/controllers/.gitkeep
|
49
|
+
create app/assets/javascripts/views
|
50
|
+
create app/assets/javascripts/views/.gitkeep
|
51
|
+
create app/assets/javascripts/helpers
|
52
|
+
create app/assets/javascripts/helpers/.gitkeep
|
53
|
+
create app/assets/javascripts/templates
|
54
|
+
create app/assets/javascripts/templates/.gitkeep
|
55
|
+
create app/assets/javascripts/app.js
|
56
|
+
|
57
|
+
If you want to avoid `.gitkeep` files, use the `skip git` option like
|
58
|
+
this: `rails g ember:bootstrap -g`.
|
59
|
+
|
60
|
+
Ask Rails to serve HandlebarsJS and pre-compile templates to Ember
|
61
|
+
by putting each template in a dedicated ".js.hjs", ".hbs" or ".handlebars" file
|
62
|
+
(e.g. `app/assets/javascripts/templates/admin_panel.handlebars`)
|
63
|
+
and including the assets in your layout:
|
64
|
+
|
65
|
+
<%= javascript_include_tag "templates/admin_panel" %>
|
66
|
+
|
67
|
+
If you want to strip template root from template names, add `templates_root` option to your application configuration block :
|
68
|
+
|
69
|
+
config.handlebars.templates_root = 'templates'
|
70
|
+
|
71
|
+
The result will be like this :
|
72
|
+
|
73
|
+
Ember.TEMPLATES['admin_panel'] = "...";
|
74
|
+
|
75
|
+
If you want a different path separator in template names add `templates_path_separator` option to your application configuration block :
|
76
|
+
|
77
|
+
config.handlebars.templates_path_separator = '-'
|
78
|
+
|
79
|
+
The result will be like this :
|
80
|
+
|
81
|
+
Ember.TEMPLATES['templates-admin_panel'] = "...";
|
82
|
+
|
83
|
+
Default behavior for ember-rails is to precompile handlebars templates only in production environment.
|
84
|
+
If you don't want this behavior you can turn it off in your application configuration block :
|
85
|
+
|
86
|
+
config.handlebars.precompile = false
|
87
|
+
|
88
|
+
Bundle all templates together thanks to Sprockets,
|
89
|
+
e.g create `app/assets/javascripts/templates/all.js` with:
|
90
|
+
|
91
|
+
//= require_tree .
|
92
|
+
|
93
|
+
Now a single line in the layout loads everything:
|
94
|
+
|
95
|
+
<%= javascript_include_tag "templates/all" %>
|
96
|
+
|
97
|
+
If you use Slim or Haml templates, you can use handlebars filter :
|
98
|
+
|
99
|
+
:handlebars
|
100
|
+
{{#view Ember.Button}}OK{{/view}}
|
101
|
+
|
102
|
+
It will be translated as :
|
103
|
+
|
104
|
+
<script type="text/x-handlebars">
|
105
|
+
{{#view Ember.Button}}OK{{/view}}
|
106
|
+
</script>
|
107
|
+
|
108
|
+
## Note on Patches/Pull Requests
|
109
|
+
|
110
|
+
1. Fork the project.
|
111
|
+
2. Make your feature addition or bug fix.
|
112
|
+
3. Add tests for it. This is important so I don't break it in a future version unintentionally.
|
113
|
+
4. Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
114
|
+
5. Send me a pull request. Bonus points for topic branches.
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'ember_rails'
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Haml
|
2
|
+
module Filters
|
3
|
+
module Handlebars
|
4
|
+
include Base
|
5
|
+
def render_with_options(text, options)
|
6
|
+
type = "type=#{options[:attr_wrapper]}text/x-handlebars#{options[:attr_wrapper]}"
|
7
|
+
"<script #{type}>\n#{text.rstrip}\n</script>"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
// DOM
|
2
|
+
var Element = {};
|
3
|
+
Element.firstChild = function () { return Element; };
|
4
|
+
Element.innerHTML = function () { return Element; };
|
5
|
+
|
6
|
+
var document = { createRange: false, createElement: function() { return Element; } };
|
7
|
+
var window = this;
|
8
|
+
this.document = document;
|
9
|
+
|
10
|
+
// null out console.log and console.warn to avoid unexpected output
|
11
|
+
if (window.console) {
|
12
|
+
window.console.warn = function() {};
|
13
|
+
window.console.log = function() {};
|
14
|
+
}
|
15
|
+
|
16
|
+
//// jQuery
|
17
|
+
var jQuery = window.jQuery = function() { return jQuery; };
|
18
|
+
jQuery.ready = function() { return jQuery; };
|
19
|
+
jQuery.inArray = function() { return jQuery; };
|
20
|
+
jQuery.jquery = "1.7.2";
|
21
|
+
jQuery.event = {fixHooks: {}};
|
22
|
+
|
23
|
+
// Precompiler
|
24
|
+
var EmberRails = {
|
25
|
+
precompile: function(string) {
|
26
|
+
return Ember.Handlebars.precompile(string).toString();
|
27
|
+
}
|
28
|
+
};
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'execjs'
|
2
|
+
|
3
|
+
module Ember
|
4
|
+
module Handlebars
|
5
|
+
class Source
|
6
|
+
class << self
|
7
|
+
def precompiler_path
|
8
|
+
File.expand_path(File.join(__FILE__, '../assets/ember-precompiler.js'))
|
9
|
+
end
|
10
|
+
|
11
|
+
def vendor_path
|
12
|
+
path = ::Rails.root.nil? ? '' : ::Rails.root.join('vendor/assets/javascripts/ember.js')
|
13
|
+
|
14
|
+
if !File.exists?(path)
|
15
|
+
path = File.expand_path(File.join(__FILE__, '../../../../vendor/ember/production/ember.js'))
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def path
|
20
|
+
@path ||= ENV['EMBER_SOURCE_PATH'] || vendor_path
|
21
|
+
end
|
22
|
+
|
23
|
+
def contents
|
24
|
+
@contents ||= begin
|
25
|
+
config = ::Rails.application.config.ember
|
26
|
+
handlebars = File.read(config.handlebars_location)
|
27
|
+
ember = File.read(config.ember_location)
|
28
|
+
precompiler = File.read(precompiler_path)
|
29
|
+
|
30
|
+
[precompiler, handlebars, ember].join("\n")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def handlebars_version
|
35
|
+
@handlebars_version ||= contents[/^Handlebars.VERSION = "([^"]*)"/, 1]
|
36
|
+
end
|
37
|
+
|
38
|
+
def ember_version
|
39
|
+
@ember_version ||= contents[/^Ember.VERSION = '([^']*)'/, 1]
|
40
|
+
end
|
41
|
+
|
42
|
+
def context
|
43
|
+
@context ||= ExecJS.compile(contents)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class << self
|
49
|
+
def compile(template)
|
50
|
+
Source.context.call('EmberRails.precompile', template)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'sprockets'
|
2
|
+
require 'sprockets/engines'
|
3
|
+
require 'ember/handlebars/source'
|
4
|
+
|
5
|
+
module Ember
|
6
|
+
module Handlebars
|
7
|
+
class Template < Tilt::Template
|
8
|
+
def self.default_mime_type
|
9
|
+
'application/javascript'
|
10
|
+
end
|
11
|
+
|
12
|
+
def prepare; end
|
13
|
+
|
14
|
+
def evaluate(scope, locals, &block)
|
15
|
+
if scope.pathname.to_s =~ /\.raw\.(handlebars|hjs|hbs)/
|
16
|
+
"Ember.TEMPLATES[#{template_path(scope.logical_path).inspect}] = Handlebars.compile(#{indent(data).inspect});\n"
|
17
|
+
else
|
18
|
+
template = mustache_to_handlebars(scope, data)
|
19
|
+
|
20
|
+
if configuration.precompile
|
21
|
+
func = Ember::Handlebars.compile(template)
|
22
|
+
"Ember.TEMPLATES[#{template_path(scope.logical_path).inspect}] = Ember.Handlebars.template(#{func});\n"
|
23
|
+
else
|
24
|
+
"Ember.TEMPLATES[#{template_path(scope.logical_path).inspect}] = Ember.Handlebars.compile(#{indent(template).inspect});\n"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def mustache_to_handlebars(scope, template)
|
32
|
+
if scope.pathname.to_s =~ /\.mustache\.(handlebars|hjs|hbs)/
|
33
|
+
template.gsub(/\{\{(\w[^\}\}]+)\}\}/){ |x| "{{unbound #{$1}}}" }
|
34
|
+
else
|
35
|
+
template
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def template_path(path)
|
40
|
+
root = configuration.templates_root
|
41
|
+
|
42
|
+
unless root.blank?
|
43
|
+
path.gsub!(/^#{Regexp.quote(root)}\/?/, '')
|
44
|
+
end
|
45
|
+
|
46
|
+
path = path.split('/')
|
47
|
+
|
48
|
+
path.join(configuration.templates_path_separator)
|
49
|
+
end
|
50
|
+
|
51
|
+
def configuration
|
52
|
+
::Rails.configuration.handlebars
|
53
|
+
end
|
54
|
+
|
55
|
+
def indent(string)
|
56
|
+
string.gsub(/$(.)/m, "\\1 ").strip
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'ember/handlebars/template'
|
2
|
+
|
3
|
+
module Ember
|
4
|
+
module Rails
|
5
|
+
class Engine < ::Rails::Engine
|
6
|
+
config.handlebars = ActiveSupport::OrderedOptions.new
|
7
|
+
|
8
|
+
config.handlebars.precompile = true
|
9
|
+
config.handlebars.templates_root = "templates"
|
10
|
+
config.handlebars.templates_path_separator = '/'
|
11
|
+
|
12
|
+
initializer "ember_rails.setup", :group => :all do |app|
|
13
|
+
require 'ember/filters/slim' if defined? Slim
|
14
|
+
require 'ember/filters/haml' if defined? Haml
|
15
|
+
|
16
|
+
app.assets.register_engine '.handlebars', Ember::Handlebars::Template
|
17
|
+
app.assets.register_engine '.hbs', Ember::Handlebars::Template
|
18
|
+
app.assets.register_engine '.hjs', Ember::Handlebars::Template
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/ember_rails.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'ember/rails/version'
|
3
|
+
require 'ember/version'
|
4
|
+
require 'ember/handlebars/version'
|
5
|
+
require 'ember/rails/engine'
|
6
|
+
|
7
|
+
module Ember
|
8
|
+
module Rails
|
9
|
+
class Railtie < ::Rails::Railtie
|
10
|
+
config.ember = ActiveSupport::OrderedOptions.new
|
11
|
+
|
12
|
+
generators do |app|
|
13
|
+
app ||= ::Rails.application # Rails 3.0.x does not yield `app`
|
14
|
+
|
15
|
+
app.config.generators.assets = false
|
16
|
+
|
17
|
+
::Rails::Generators.configure!(app.config.generators)
|
18
|
+
::Rails::Generators.hidden_namespaces.uniq!
|
19
|
+
require "generators/ember/resource_override"
|
20
|
+
end
|
21
|
+
|
22
|
+
initializer "ember_rails.setup_vendor", :after => "ember_rails.setup", :group => :all do |app|
|
23
|
+
if variant = app.config.ember.variant
|
24
|
+
# Add the gem's vendored ember to the end of the asset search path
|
25
|
+
ember_path = File.expand_path("../../vendor/ember/#{variant}", __FILE__)
|
26
|
+
app.config.assets.paths.push(ember_path.to_s)
|
27
|
+
|
28
|
+
# Allow a local variant override
|
29
|
+
ember_path = app.root.join("vendor/assets/ember/#{variant}")
|
30
|
+
app.config.assets.paths.unshift(ember_path.to_s) if ember_path.exist?
|
31
|
+
else
|
32
|
+
warn "No ember.js variant was specified in your config environment."
|
33
|
+
warn "You can set a specific variant in your application config in "
|
34
|
+
warn "order for sprockets to locate ember's assets:"
|
35
|
+
warn ""
|
36
|
+
warn " config.ember.variant = :development"
|
37
|
+
warn ""
|
38
|
+
warn "Valid values are :development and :production"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
initializer "ember_rails.find_ember", :after => "ember_rails.setup_vendor", :group => :all do |app|
|
43
|
+
config.ember.ember_location ||= location_for(app, "ember.js")
|
44
|
+
config.ember.handlebars_location ||= location_for(app, "handlebars.js")
|
45
|
+
end
|
46
|
+
|
47
|
+
def location_for(app, file)
|
48
|
+
path = app.config.assets.paths.find do |dir|
|
49
|
+
Pathname.new(dir).join(file).exist?
|
50
|
+
end
|
51
|
+
|
52
|
+
File.join(path, file) if path
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'generators/ember/generator_helpers'
|
2
|
+
|
3
|
+
module Ember
|
4
|
+
module Generators
|
5
|
+
class BootstrapGenerator < ::Rails::Generators::Base
|
6
|
+
include Ember::Generators::GeneratorHelpers
|
7
|
+
|
8
|
+
source_root File.expand_path("../../templates", __FILE__)
|
9
|
+
|
10
|
+
desc "Creates a default Ember.js folder layout in app/assets/javascripts/ember"
|
11
|
+
|
12
|
+
class_option :skip_git, :type => :boolean, :aliases => "-g", :default => false, :desc => "Skip Git keeps"
|
13
|
+
|
14
|
+
def inject_ember
|
15
|
+
application_file = "app/assets/javascripts/application.js"
|
16
|
+
|
17
|
+
inject_into_file(application_file, :before => "//= require_tree") do
|
18
|
+
dependencies = [
|
19
|
+
# this should eventually become handlebars-runtime when we remove
|
20
|
+
# the runtime dependency on compilation
|
21
|
+
"//= require handlebars",
|
22
|
+
"//= require ember",
|
23
|
+
"//= require ember-data",
|
24
|
+
"//= require_self",
|
25
|
+
"//= require #{application_name.underscore}",
|
26
|
+
"#{application_name.camelize} = Ember.Application.create();"
|
27
|
+
]
|
28
|
+
dependencies.join("\n").concat("\n")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def create_dir_layout
|
33
|
+
%W{models controllers views routes helpers templates}.each do |dir|
|
34
|
+
empty_directory "#{ember_path}/#{dir}"
|
35
|
+
create_file "#{ember_path}/#{dir}/.gitkeep" unless options[:skip_git]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def create_app_file
|
40
|
+
template "app.js", "#{ember_path}/#{application_name.underscore}.js"
|
41
|
+
end
|
42
|
+
|
43
|
+
def create_router_file
|
44
|
+
template "router.js", "#{ember_path}/routes/app_router.js"
|
45
|
+
end
|
46
|
+
|
47
|
+
def create_store_file
|
48
|
+
template "store.js", "#{ember_path}/store.js"
|
49
|
+
end
|
50
|
+
|
51
|
+
def create_app_stubs
|
52
|
+
generate "ember:view", "application"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|