我目前正在尝试使用 ExecJS 为我正在开发的产品之一运行 Handlebars(注意:我知道 handlebars.rb gem 非常酷,并且我使用了一段时间,但是在 Windows 上安装它存在问题,所以我尝试了另一种自制的解决方案)。
我遇到的问题之一是 Javascript 上下文没有保留在对 ExecJS 的每个“调用”之间。
这里是我实例化 @js 属性的代码:
class Context
attr_reader :js, :partials, :helpers
def initialize
src = File.open(::Handlebars::Source.bundled_path, 'r').read
@js = ExecJS.compile(src)
end
end
这是一个显示问题的测试:
let(:ctx) { Hiptest::Handlebars::Context.new }
it "does not keep context properly (or I'm using the tool wrong" do
ctx.js.eval('my_variable = 42')
expect(ctx.js.eval('my_variable')).to eq(42)
end
现在当我运行它时:
rspec spec/handlebars_spec.rb:10 1 ↵
I, [2015-02-21T16:57:30.485774 #35939] INFO -- : Not reporting to Code Climate because ENV['CODECLIMATE_REPO_TOKEN'] is not set.
Run options: include {:locations=>{"./spec/handlebars_spec.rb"=>[10]}}
F
Failures:
1) Hiptest::Handlebars Context does not keep context properly (or I'm using the tool wrong
Failure/Error: expect(ctx.js.eval('my_variable')).to eq(42)
ExecJS::ProgramError:
ReferenceError: Can't find variable: my_variable
注意:我用“exec”而不是“eval”遇到了同样的问题。
这是一个愚蠢的例子。我真正想做的是运行“Handlebars.registerPartial”,然后运行“Handlebars.compile”。但是,当尝试使用模板中的部分时,它会失败,因为之前注册的部分丢失了。
请注意,我找到了一种解决方法,但我觉得它很丑:/
def register_partial(name, content)
@partials[name] = content
end
def call(*args)
@context.js.call([
"(function (partials, helpers, tmpl, args) {",
" Object.keys(partials).forEach(function (key) {",
" Handlebars.registerPartial(key, partials[key]);",
" })",
" return Handlebars.compile(tmpl).apply(null, args);",
"})"].join("\n"), @partials, @template, args)
end
关于如何解决这个问题的任何想法?