Ember.js和RequireJS

有没有人有RequireJS和Ember.js很多成功? 看到Ember喜欢把它的结构分配给一个全局对象,我发现它可以真正的与Requirejs对接。 像Ember的LAB.js会更好吗?

编辑 (2012-01-30):我推出了一个更完整的例子在一个回购 bitbucket。

我成功地使用了RequireJS来加载Ember.js以及date时间插件( github )。 Ember命名空间本身保持全局,但是我所有的应用程序数据,包括我的Ember.Application实例,都通过RequireJS保存在模块中。 我也使用“文本”加载句柄模板 插入。

我还没有任何问题,但这不是一个完整的应用程序,更多的是一个概念certificate。 以下是我如何使其工作。 我可以直接使用Safari加载我的应用程序,而不需要服务器。 你仍然需要一个服务器来加载它,它不允许JavaScript从文件系统加载文件。

1)因为Ember.js使用BPM / Spade,我不能使用repo的克隆。 相反,我正在编译的版本在一个模块中:

lib / ember.js

define(['jquery', 'plugins/order!lib/ember-0.9.3.js', 'plugins/order!lib/ember-datetime.js'], function() { return Ember; }); 

请注意,这本身不会将Ember从全局范围中隐藏起来。 但是我正在设置,所以如果将来我可以隐藏它,那么依赖于这个模块的其他模块仍然可以工作。

2)模块可以像这样引用Ember:

app / core.js

 define(['lib/ember'], function(Em) { MyApp = Em.Application.create({ VERSION: "0.0.1" }); return MyApp; }); 

在这里,“Em”可以被命名为别的东西; 它不直接引用全局variables,而是来自lib / ember.js中定义的模块。

3)要通过Ember访问,车把必须注册:

app / templates / my-template.handlebars

 MyApp v{{MyApp.VERSION}}. 

app / views / my-view.js

 define(['lib/ember', 'plugins/text!app/templates/my-template.handlebars'], function(Em, myTemplateSource) { Em.TEMPLATES["my-template"] = Em.Handlebars.compile(myTemplateSource); var myView = Em.View.create({ templateName: "my-template"; }); return myView; }); 

4)我使用require-jquery.js (捆绑在一起的jQuery和RequireJS)。

有一个更好的方法来包含一个可以有多个模板块的句柄文件。 其中可以得到编译和可在一个包括。

例如:您有以下Handlebars模板文件:

../templates/sample.handlebars

 <div><!-- just a filler html tag. Wont show up in your page --> <script type="text/x-handlebars" data-template-name="template1"> Some Html or Template Content ... </script> <script type="text/x-handlebars" data-template-name="template2"> Some Html or Template Content ... </script> <script type="text/x-handlebars" data-template-name="template3"> Some Html or Template Content ... </script> </div> 

../views/sampleView.js

 define([ 'jquery', 'lib/ember', 'plugins/text!../templates/sample.handlebars'], function($, Em, myTemplateSource) { // Bootstrap method accepts a context element under which all handlebars // template blocks are defined. The filler <div> in this case. // Compiles and assigns to the EM.TEMPLATES hash correctly. Em.Handlebars.bootstrap($(myTemplateSource)); var myView = Em.View.create({ templateName: "template1"; }); return myView; }); 

我刚刚上传到Github的EmberJS + RequireJS入门套件,你可以检查它https://github.com/fernandogmar/Emberjs-RequireJS

任何好的build议将不胜感激。 玩的开心!

Ember CLI支持转换到AMD的ES6模块语法。 似乎社区正在使用Ember CLI作为使用Ember的首选方式。

http://www.ember-cli.com

含羞草有几个很好的示例项目使用Ember和Require.js。 这里有一个结帐: https : //github.com/dbashford/mimosa-ember-emblem-templates 。 让它进入的说明在README中。