使用nodejs预先编译Emberjs Handlebar模板的简单方法? [英] Easy way to precompile Emberjs Handlebar templates with nodejs?

查看:117
本文介绍了使用nodejs预先编译Emberjs Handlebar模板的简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很喜欢emberjs,并希望在我的一些小型移动应用程序中进行下一步,并在我的构建过程中预编译我的Ember / Handlebars模板。



我宁愿远离Ruby的混乱,并希望使用node.js,因为我更喜欢使用它。



我相信我想使用的是Ember.Handlebars.precompile,但不幸的是我无法在节点环境中加载规范的ember.js文件。来自节点的初始尝试的示例repl:

 > var e = require('./ ember'); 
参考错误:窗口未定义
在/Users/jeremyosborne/git/projects/ldls/client/lib/emberjs/src/ember.js:5:1
在Object。<匿名> (/Users/jeremyosborne/git/projects/ldls/client/lib/emberjs/src/ember.js:1596:2)
---堆栈跟踪,你会得到想法---

我想我已经弄清楚如何在我的代码中设置它们,以便我的意见正确地工作,我只需要在浏览器DOM之外的环境中编译它们。



代替将规范化的ember.js加载到节点中,是否有一组特定的文件我可以从ember repo中取出并使用来编译我的模板?



编辑
我做了一个很好的kluge修复但是为了可维护性而获得F。我抓住所有的 Handlebars 代码减去对窗口对象的引用。然后我跟着 Ember.Handlebars.Compiler 代码,用对象取代 Ember.create .create ,导出我的 Ember 对象,并且中提琴的东西在节点中似乎很棒(因为它的作品和所生成的功能是模板)。但是,由于上述维护,我不认为这是我自己的问题的答案,所以仍然开放答案。



编辑2
以上证明是完全失败的。也许程序有问题,但是使用 Ember.Handlebars.precompile Ember.Handlebars.compile 不会工作。模板得到了,但是当我在代码中使用与Ember.TEMPLATES相连的预编译模板时,它们不起作用。在emberjs.com网站上建议的时候,我似乎只能在修改后的脚本标签中明确传递模板,才能使模板工作。



编辑3
我想出了我做错了什么我的答案在下面。

解决方案

找到一个很好的解决方案,我的问题似乎很容易维系,我会考虑我的问题解决了。



我是如何解决问题的:




  • 的代码我需要预编译ember模板。

  • 简单预编译过程的大纲:

    • 使用加载代码var Ember = require('./ my_ember_precompiler'获取您的模板作为字符串,并使用 var templateString = Ember.Handlebars.precompile(str).toString编译它们。。 ()

    • 这与应用程序不同,但Ember似乎需要注册预编译的模板。加载后,对于每个创建的模板,我们需要注册我们的模板。在调用$ code> Handlebars.template()的基础上打包templateString,并确保将包装的函数添加到 Ember.TEMPLATES 对象




上面的脚本在自动化时是无痛的。 / p>

I'm enjoying emberjs a lot and would like to take the next step in a couple of my small, mobile apps and precompile my Ember/Handlebars templates as part of my build process.

I'd prefer to stay away from messing with Ruby and would like to use node.js as I'm more comfortable with using it.

I believe what I want to use is Ember.Handlebars.precompile, but unfortunately I'm unable to load the canonical ember.js file in a node environment. Example of a naive attempt from the node repl:

> var e = require('./ember');
ReferenceError: window is not defined
    at /Users/jeremyosborne/git/projects/ldls/client/lib/emberjs/src/ember.js:5:1
    at Object.<anonymous> (/Users/jeremyosborne/git/projects/ldls/client/lib/emberjs/src/ember.js:1596:2)
    --- stack trace, you get the idea ---

I think I've already figured out how to set them up in my code so that they work correctly with my views, I just want to compile them in an environment outside of a browser DOM.

In lieu of getting the canonical ember.js to load in node, are there a specific set of files that I can pluck from the ember repo and use to compile my templates?

EDIT I did a kluge fix that works great but gets an F for maintainability. I grabbed all the Handlebars code minus the reference to the window object. Then I followed with the Ember.Handlebars.Compiler code, replacing Ember.create with Object.create, exporting my Ember object, and viola things work seemingly great in node (as in it works and the functions produced are templates). But I don't consider this an answer to my own question due to the aforementioned maintainafail, so still open for answers.

EDIT 2 The above turns out to be a total fail. Perhaps there's something wrong in the procedure, but using Ember.Handlebars.precompile or Ember.Handlebars.compile doesn't work. The templates get made, but when I use the precompiled templates attached to Ember.TEMPLATES in my code, they do not work. I only seem to be able to get templates to work when they are explicitly passed in the modified script tags, as suggested on the emberjs.com site.

EDIT 3 I figured out what I was doing wrong. My answer is below.

解决方案

Found a good enough solution to my problem that seems easy enough to maintain that I'll consider my problem solved.

Here's how I solved things:

  • Grab the minimal amount of code I need to precompile the ember templates.
  • Outline of the simple precompile procedure:
    • Load up the code with a var Ember = require('./my_ember_precompiler').Ember.
    • Get your templates as strings and compile them with var templateString = Ember.Handlebars.precompile(str).toString().
    • This will be different from app to app, but Ember seems to require registration of precompiled templates. After loading, for every created template, we need to register our templates. Basically wrap templateString in a call to Handlebars.template() and make sure this wrapped function is added to the Ember.TEMPLATES object.

The above is painless when it's automated in a script.

这篇关于使用nodejs预先编译Emberjs Handlebar模板的简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆