Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'refs/heads/barber'

  • Loading branch information...
commit bc359f9625d5961d9cd809f8a0e916d1a25f5b56 2 parents c2a7d69 + f2d2cb5
Paul Chavard tchak authored
1  ember-rails.gemspec
View
@@ -14,6 +14,7 @@ Gem::Specification.new do |s|
s.add_dependency "execjs", [">= 1.2"]
s.add_dependency "railties", ["~> 3.1"]
s.add_dependency "active_model_serializers"
+ s.add_dependency "barber"
s.add_development_dependency "rails", ["~> 3.1"]
28 lib/ember/handlebars/assets/ember-precompiler.js
View
@@ -1,28 +0,0 @@
-// DOM
-var Element = {};
-Element.firstChild = function () { return Element; };
-Element.innerHTML = function () { return Element; };
-
-var document = { createRange: false, createElement: function() { return Element; } };
-var window = this;
-this.document = document;
-
-// null out console.log and console.warn to avoid unexpected output
-if (window.console) {
- window.console.warn = function() {};
- window.console.log = function() {};
-}
-
-//// jQuery
-var jQuery = window.jQuery = function() { return jQuery; };
-jQuery.ready = function() { return jQuery; };
-jQuery.inArray = function() { return jQuery; };
-jQuery.jquery = "1.7.2";
-jQuery.event = {fixHooks: {}};
-
-// Precompiler
-var EmberRails = {
- precompile: function(string) {
- return Ember.Handlebars.precompile(string).toString();
- }
-};
54 lib/ember/handlebars/source.rb
View
@@ -1,54 +0,0 @@
-require 'execjs'
-
-module Ember
- module Handlebars
- class Source
- class << self
- def precompiler_path
- File.expand_path(File.join(__FILE__, '../assets/ember-precompiler.js'))
- end
-
- def vendor_path
- path = ::Rails.root.nil? ? '' : ::Rails.root.join('vendor/assets/javascripts/ember.js')
-
- if !File.exists?(path)
- path = File.expand_path(File.join(__FILE__, '../../../../vendor/ember/production/ember.js'))
- end
- end
-
- def path
- @path ||= ENV['EMBER_SOURCE_PATH'] || vendor_path
- end
-
- def contents
- @contents ||= begin
- config = ::Rails.application.config.ember
- handlebars = File.read(config.handlebars_location)
- ember = File.read(config.ember_location)
- precompiler = File.read(precompiler_path)
-
- [precompiler, handlebars, ember].join("\n")
- end
- end
-
- def handlebars_version
- @handlebars_version ||= contents[/^Handlebars.VERSION = "([^"]*)"/, 1]
- end
-
- def ember_version
- @ember_version ||= contents[/^Ember.VERSION = '([^']*)'/, 1]
- end
-
- def context
- @context ||= ExecJS.compile(contents)
- end
- end
- end
-
- class << self
- def compile(template)
- Source.context.call('EmberRails.precompile', template)
- end
- end
- end
-end
50 lib/ember/handlebars/template.rb
View
@@ -1,6 +1,6 @@
require 'sprockets'
require 'sprockets/engines'
-require 'ember/handlebars/source'
+require 'barber'
module Ember
module Handlebars
@@ -12,22 +12,58 @@ def self.default_mime_type
def prepare; end
def evaluate(scope, locals, &block)
- if scope.pathname.to_s =~ /\.raw\.(handlebars|hjs|hbs)/
- "Ember.TEMPLATES[#{template_path(scope.logical_path).inspect}] = Handlebars.compile(#{indent(data).inspect});\n"
+ target = template_target(scope)
+ raw = handlebars?(scope)
+
+ if raw
+ template = data
else
template = mustache_to_handlebars(scope, data)
+ end
- if configuration.precompile
- func = Ember::Handlebars.compile(template)
- "Ember.TEMPLATES[#{template_path(scope.logical_path).inspect}] = Ember.Handlebars.template(#{func});\n"
+ if configuration.precompile
+ if raw
+ template = precompile_handlebars(template)
+ else
+ template = precompile_ember_handlebars(template)
+ end
+ else
+ if raw
+ template = compile_handlebars(data)
else
- "Ember.TEMPLATES[#{template_path(scope.logical_path).inspect}] = Ember.Handlebars.compile(#{indent(template).inspect});\n"
+ template = compile_ember_handlebars(template)
end
end
+
+ "#{target} = #{template}\n"
end
private
+ def handlebars?(scope)
+ scope.pathname.to_s =~ /\.raw\.(handlebars|hjs|hbs)/
+ end
+
+ def template_target(scope)
+ "Ember.TEMPLATES[#{template_path(scope.logical_path).inspect}]"
+ end
+
+ def compile_handlebars(string)
+ "Handlebars.compile(#{indent(string).inspect});"
+ end
+
+ def precompile_handlebars(string)
+ Barber::FilePrecompiler.call(string)
+ end
+
+ def compile_ember_handlebars(string)
+ "Ember.Handlebars.compile(#{indent(string).inspect});"
+ end
+
+ def precompile_ember_handlebars(string)
+ Barber::Ember::FilePrecompiler.call(string)
+ end
+
def mustache_to_handlebars(scope, template)
if scope.pathname.to_s =~ /\.mustache\.(handlebars|hjs|hbs)/
template.gsub(/\{\{(\w[^\}\}]+)\}\}/){ |x| "{{unbound #{$1}}}" }
2  lib/ember/handlebars/version.rb
View
@@ -1,5 +1,3 @@
-require 'ember/handlebars/source'
-
module Ember
module Handlebars
VERSION = "1.0.rc.1"
2  lib/ember/version.rb
View
@@ -1,5 +1,3 @@
-require 'ember/handlebars/source'
-
module Ember
VERSION = "1.0-pre.2"
end
2  test/hjstemplate_test.rb
View
@@ -69,7 +69,7 @@ def with_template_root(root, sep=nil)
test "ensure new lines inside the anon function are persisted" do
get "/assets/templates/new_lines.js"
assert_response :success
- assert @response.body.include?("helpers;\n"), @response.body.inspect
+ assert @response.body.include?("helpers; data = data || {};\n"), @response.body.inspect
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.