Skip to content

Instantly share code, notes, and snippets.

@tbranyen
Created November 21, 2013 19:49
Show Gist options
  • Select an option

  • Save tbranyen/7588341 to your computer and use it in GitHub Desktop.

Select an option

Save tbranyen/7588341 to your computer and use it in GitHub Desktop.
Automatically bind Ember Classes to the Application object with AMD.
/* Ember App Attach Plugin v0.1.0
* Copyright 2013, Tim Branyen (@tbranyen)
* ember-loader.js may be freely distributed under the MIT license.
*/
define(function(require, exports) {
exports.version = "0.1.0";
exports.load = function(name, req, load, config) {
var App = require("app");
req([name], function(Class) {
var parts = name.split("/");
var fileName = parts[parts.length-1];
var titleCaseName = fileName.split("_").map(function(part) {
return part[0].toUpperCase() + part.slice(1);
}).join("");
// Attach to Ember Application.
App[titleCaseName] = Class;
return config.isBuild ? load() : load(Class);
});
};
});
require({
paths: { 'emAttach': 'em-attach' }
}, [
'app',
'emAttach!user/user_model'
], function(App, UserModel) {
console.log(App.UserModel === UserModel);
// => true
});
@tbranyen

Copy link
Copy Markdown
Author

Automatically bind Ember Classes to the Application object with AMD. It's often nice to be able to directly require an Ember Class via require('./module/some-class'), but it's annoying to have to manually attach to the application object. This provides an alternative way to load a module and attach.

@shama

shama commented Nov 21, 2013

Copy link
Copy Markdown

Check out Ember resolvers instead. This one works well with AMD: https://github.com/stefanpenner/ember-jj-abrams-resolver and I have one in progress for use with webpack: https://github.com/shama/ember-webpack-resolver

@thomasboyt

Copy link
Copy Markdown

Stef's resolver won't work with out of the box AMD because it expects some special ES6 module transpiler stuff (namely export called default instead of the actual return value).

Either way, it won't work w/ async RequireJS because async resolution is a crazy can of worms that no one wants to open right now. Easier to just concat + source maps :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment