Skip to content

Instantly share code, notes, and snippets.

@richardkmichael
Forked from slindberg/README.md
Created August 17, 2014 15:31
Show Gist options
  • Save richardkmichael/84b25c514c9a2ba54efb to your computer and use it in GitHub Desktop.
Save richardkmichael/84b25c514c9a2ba54efb to your computer and use it in GitHub Desktop.

Ember.Console

This is a set of helpers for finding the application's currently active models/routes/controllers/etc. This isn't a straightforward process because of how Ember (rightly) encapsulates application objects, but it's useful in debugging environments to be able to quickly access them. And with the beta release of Ember Data, the store is not easily accessible without helpers either.

Usage

All helpers can be called directly if you provide them an application instance:

// the `App` variable is an `Ember.Application` instance
var currentModel = Ember.Console.helpers.model(App);

But usually you'll want to inject them into the global namespace for ease of access:

// somewhere in app bootstrapping
if (Ember.ENV.DEBUG) {
  Ember.Console.injectHelpers(window, App);
}

// later in the console...
var currentModel = model();

Getting Ember Data records is easy as well:

var user = store().getById('user', '74');

Testing

These same helpers can also be very useful for integration testing. They can be exposed as normal test helpers:

// somewhere in test bootstrapping
Ember.Console.registerTestHelpers();

// later in a test...
visit('/').then(function() {
  expect(routeName()).to.equal('index');
});

Be careful! All test helpers are exposed as globals, so their generic names may conflict with simple local variable names:

// because of variable hoisting, the global helper is wiped out before it can be used
var model = model();

// nope!
expect(model).to.be.ok();

Note

The Ember.Debug namespace is taken by the Ember Inspector chrome extension. The basics of these helpers was originally taken from a testing example I cannot find any longer.

// Debug environment helpers
Ember.Console = {
helpers: {
// Return a reference to the application's DI container
container: function(app) {
return app.__container__;
},
// Return a reference to the router instance
router: function(app) {
return this.container(app).lookup('router:main');
},
// Return the current path
path: function(app) {
return this.controller(app, 'application').get('currentPath');
},
// Return a reference to the current route name
routeName: function(app) {
var parts = this.path(app).split('.');
return parts.slice(parts.length - 2).join('.');
},
// Return a reference to the current route instance, or optionally the named route
route: function(app, name) {
name || (name = this.routeName(app));
return this.container(app).lookup('route:' + name);
},
// Return a reference to the current controller instance, or optionally a named controller
controller: function(app, name) {
return name ? this.container(app).lookup('controller:' + name) : this.route(app).get('controller');
},
// Return a reference to the current model, or optionally the model of the named controller
model: function(app, name) {
var controller = name && this.controller(app, name);
return name ? controller && controller.get('model') : this.route(app).get('currentModel');
},
// Return a reference to all models in the current route chain
// NOTE: this depends on internals that are likely change in the future
models: function(app) {
return this.router(app).router.currentHandlerInfos.map(function(info) {
return info.handler.get('currentModel');
});
},
// Return a reference to the store instance
store: function(app) {
return this.container(app).lookup('store:main');
}
},
// Injects the helpers into the given object, bound to the given app
injectHelpers: function(root, app) {
Ember.keys(this.helpers).forEach(function(key) {
root[key] = this.helpers[key].bind(this.helpers, app);
}, this);
},
// Register each debug helper as a test helper
registerTestHelpers: function() {
Ember.keys(this.helpers).forEach(function(key) {
Ember.Test.registerHelper(key, this.helpers[key].bind(this.helpers));
}, this);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment