Skip to content

Instantly share code, notes, and snippets.

@wycats
Created December 31, 2012 17:04
Show Gist options
  • Save wycats/4421338 to your computer and use it in GitHub Desktop.
Save wycats/4421338 to your computer and use it in GitHub Desktop.
var DefaultView = Ember.View.extend(Ember._Metamorph);
function controllerFor(container, templateName, context) {
var controller = container.lookup('controller:' + templateName);
if (!controller) {
if (context && Ember.isArray(context)) {
controller = Ember.ArrayController.extend({ content: context });
} else if (context) {
controller = Ember.ObjectController.extend({ content: context });
} else {
controller = Ember.Controller.extend();
}
container.register('controller', templateName, controller);
controller = container.lookup('controller:' + templateName);
}
return controller;
}
Ember.Handlebars.registerHelper('render', function(name, context, options) {
Ember.assert("You must pass a template to render", arguments.length >= 2);
var container, controller, view;
if (arguments.length === 2) {
options = context;
context = undefined;
}
if (typeof context === 'string') {
context = Ember.Handlebars.get(options.contexts[1], context, options);
}
container = options.data.keywords.controller.container;
// TODO support named views here
view = DefaultView;
if (controller = options.hash.controller) {
controller = container.lookup('controller:' + controller);
controller.set('content', context);
} else {
controller = controllerFor(container, name, context);
}
options.hash.controller = controller;
options.hash.templateName = name;
Ember.Handlebars.helpers.view.call(this, view, options);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment