34

Has anyone had much success with RequireJS and Ember.js? Seeing as Ember likes to have its structure assigned to a global object I'm finding that it can really butt heads with Requirejs. Would something like LAB.js work better for Ember?

MilkyWayJoe
  • 9,082
  • 2
  • 38
  • 53
Jonesy
  • 425
  • 1
  • 5
  • 5
  • I also found requirejs hard to use with other libraries and vice-versa. That is why I created a library which is much easier to use and is tested with angular. There is a demo application at the bottom: gngeorgiev.github.io/Modulerr.js You can also combine all scripts into one without the dependency to Modulerr.js – Georgi-it Aug 10 '14 at 21:30

5 Answers5

31

EDIT (2012-01-30): I pushed a more complete example in a repo on bitbucket.

I have successfully used RequireJS to load Ember.js as well as the datetime addon (github). The Ember namespace itself stays global, but all of my application's data, including my instance of Ember.Application, is kept within modules through RequireJS. I'm also loading the handlebars templates using the 'text!' plugin.

I haven't had any issue yet, but this is not a complete application, more of a proof of concept. Here's how I made it work. I can load my application directly with Safari without the need of a server. You would still need a server to load it with Chrome, which doesn't let JavaScript load files from the filesystem.

1) Because Ember.js uses BPM/Spade, I couldn't use a clone of the repo. Instead I'm wrapping the compiled version within a module:

lib/ember.js:

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

Note that this in itself doesn't hide Ember from the global scope. But I'm setting things up so that if, in the future, I'm able to do hide it, every other module which depends on this module will still work as-is.

2) Modules can refer to Ember like so:

app/core.js:

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

Here, "Em" could have been named something else; it doesn't refer directly to the global variable, but comes from the module defined in lib/ember.js.

3) To be accessible by Ember, handlebars have to be registered:

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) I'm using require-jquery.js (jQuery and RequireJS bundled together).

alexandru.topliceanu
  • 2,364
  • 2
  • 27
  • 38
Claude Précourt
  • 907
  • 11
  • 11
  • When following your approach I get this error : Error: Module name 'ember-runtime' has not been loaded yet for context: _ Do you have any ideas how to resolve that ? i'm out of ideas :) – Yaniv De Ridder Jan 18 '12 at 09:07
  • Maybe you have a public repo where i can look at your sample ? this would be handy! – Yaniv De Ridder Jan 18 '12 at 09:26
  • @claude-precourt It looks like MyApp is declared as a global variable since you are not using the var keyword. Are you sure this works also without using the global context? – Timo Westkämper Jan 19 '12 at 08:04
  • 1
    @YanivDeRidder : I got the same error when I forgot to remove the 'require' statement at the top of the datetime addon. And no, I don't have a repo with this code available yet. But I am working on a simple demo app which I will post somewhere. – Claude Précourt Jan 24 '12 at 16:58
  • @TimoWestkämper: You are right about the global scope of MyApp. If MyApp isn't global, the handlebars templates can't refer to it. I haven't found a way to manage the context of those templates. – Claude Précourt Jan 24 '12 at 17:00
  • ember v0.9.4 requires jquery. An edit is required in `lib/ember.js` to include jquery v1.7.x as dependency – alexandru.topliceanu Jan 31 '12 at 09:18
  • This is fantastic! but how do you prevent caching of the views? I'm having a real hard time keeping them up to date the caching is really persistent – user391986 Sep 12 '12 at 16:29
8

There is a better way to include a handlebar file which can have multiple template blocks. Which can get compiled and available in one include.

for eg: You have the following Handlebars template file:

../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;
});
  • 1
    Very elegant. Is using requireJS for Ember a "best practice", or more of a way to organize huge projects? I'm really looking for a way to structure my project, but I have a bad feeling with having a proxy pre-loading everything all the time. On the other hand, it's just clearer that way. – nembleton Oct 28 '12 at 10:14
3

I've just uploaded to github a starter-kit for EmberJS+RequireJS, You could check it https://github.com/fernandogmar/Emberjs-RequireJS

Any good suggestions will be highly appreciated. Have Fun!

Community
  • 1
  • 1
Fernando Gm
  • 1,191
  • 9
  • 13
3

Ember CLI supports ES6 module syntax which transpiles to AMD. Seems like the community is getting behind Ember CLI as the preferred way to use Ember.

http://www.ember-cli.com

Christos
  • 1,297
  • 10
  • 9
0

Mimosa has a few good example projects using Ember and Require.js. Here's one to checkout: https://github.com/dbashford/mimosa-ember-emblem-templates. The instructions to get it going it are in the README.

David Bashford
  • 605
  • 4
  • 6