Permalink
Browse files

Remove support for inline anonymous templates.

Default a template without a name to application.
  • Loading branch information...
krisselden committed Sep 15, 2012
1 parent 8820b43 commit bb149dcbb7df91866fce10e6dbec78c3e439d0ee
Showing with 30 additions and 90 deletions.
  1. +6 −42 packages/ember-handlebars/lib/loader.js
  2. +24 −48 packages/ember-handlebars/tests/loader_test.js
@@ -39,49 +39,14 @@ Ember.Handlebars.bootstrap = function(ctx) {
// Get the name of the script, used by Ember.View's templateName property.
// First look for data-template-name attribute, then fall back to its
// id if no name is found.
templateName = script.attr('data-template-name') || script.attr('id'),
template = compile(script.html()),
view, viewPath, elementId, options;
templateName = script.attr('data-template-name') || script.attr('id') || 'application',
template = compile(script.html());
if (templateName) {
// For templates which have a name, we save them and then remove them from the DOM
Ember.TEMPLATES[templateName] = template;
// For templates which have a name, we save them and then remove them from the DOM
Ember.TEMPLATES[templateName] = template;
// Remove script tag from DOM
script.remove();
} else {
if (script.parents('head').length !== 0) {
// don't allow inline templates in the head
throw new Ember.Error("Template found in <head> without a name specified. " +
"Please provide a data-template-name attribute.\n" +
script.html());
}
// For templates which will be evaluated inline in the HTML document, instantiates a new
// view, and replaces the script tag holding the template with the new
// view's DOM representation.
//
// Users can optionally specify a custom view subclass to use by setting the
// data-view attribute of the script tag.
viewPath = script.attr('data-view');
view = viewPath ? Ember.get(viewPath) : Ember.View;
// Get the id of the script, used by Ember.View's elementId property,
// Look for data-element-id attribute.
elementId = script.attr('data-element-id');
options = { template: template };
if (elementId) { options.elementId = elementId; }
view = view.create(options);
view._insertElementLater(function() {
script.replaceWith(this.$());
// Avoid memory leak in IE
script = null;
});
}
// Remove script tag from DOM
script.remove();
});
};
@@ -100,5 +65,4 @@ function bootstrap() {
from the DOM after processing.
*/
Ember.$(document).ready(bootstrap);
Ember.onLoad('application', bootstrap);
@@ -7,69 +7,45 @@ module("test Ember.Handlebars.bootstrap", {
}
});
test('template with data-template-name should add a new template to Ember.TEMPLATES', function() {
Ember.$('#qunit-fixture').html('<script type="text/x-handlebars" data-template-name="funkyTemplate" >{{Tobias.firstName}} {{Tobias.lastName}}</script>');
function checkTemplate(templateName) {
Ember.run(function() {
Ember.Handlebars.bootstrap(Ember.$('#qunit-fixture'));
Tobias = Ember.Object.create({
firstName: 'Tobias',
lastName: 'Fünke'
});
});
ok(Ember.TEMPLATES['funkyTemplate'], 'template with name funkyTemplate available');
equal(Ember.$('#qunit-fixture').text(), '', 'no template content is added');
});
test('template with id instead of data-template-name should add a new template to Ember.TEMPLATES', function() {
Ember.$('#qunit-fixture').html('<script type="text/x-handlebars" id="funkyTemplate" >{{Tobias.firstName}} takes {{Tobias.drug}}</script>');
Ember.run(function() {
Ember.Handlebars.bootstrap(Ember.$('#qunit-fixture'));
Tobias = Ember.Object.create({
var template = Ember.TEMPLATES[templateName];
ok(template, 'template is available on Ember.TEMPLATES');
equal(Ember.$('#qunit-fixture script').length, 0, 'script removed');
var view = Ember.View.create({
template: template,
context: {
firstName: 'Tobias',
drug: 'teamocil'
});
}
});
ok(Ember.TEMPLATES['funkyTemplate'], 'template with name funkyTemplate available');
equal(Ember.$('#qunit-fixture').text(), '', 'no template content is added');
});
test('inline template should be added', function() {
Ember.$('#qunit-fixture').html('<script type="text/x-handlebars" >{{Tobias.firstName}} {{Tobias.lastName}}</script>');
Ember.run(function() {
Ember.Handlebars.bootstrap(Ember.$('#qunit-fixture'));
Tobias = Ember.Object.create({
firstName: 'Tobias',
lastName: 'Fünke'
});
view.createElement();
});
equal(Ember.$('#qunit-fixture').text(), 'Tobias Fünke', 'template is rendered');
});
test('template with data-element-id should add an id attribute to the view', function() {
Ember.$('#qunit-fixture').html('<script type="text/x-handlebars" data-element-id="application">Hello World !</script>');
equal(view.$().text(), 'Tobias takes teamocil', 'template works');
Ember.run(function() {
Ember.Handlebars.bootstrap(Ember.$('#qunit-fixture'));
view.destroy();
});
}
equal(Ember.$('#qunit-fixture #application').text(), 'Hello World !', 'view exists with id');
test('template with data-template-name should add a new template to Ember.TEMPLATES', function() {
Ember.$('#qunit-fixture').html('<script type="text/x-handlebars" data-template-name="funkyTemplate">{{firstName}} takes {{drug}}</script>');
checkTemplate('funkyTemplate');
});
test('template without data-element-id should still get an attribute', function() {
Ember.$('#qunit-fixture').html('<script type="text/x-handlebars">Hello World!</script>');
test('template with id instead of data-template-name should add a new template to Ember.TEMPLATES', function() {
Ember.$('#qunit-fixture').html('<script type="text/x-handlebars" id="funkyTemplate" >{{firstName}} takes {{drug}}</script>');
Ember.run(function() {
Ember.Handlebars.bootstrap(Ember.$('#qunit-fixture'));
});
checkTemplate('funkyTemplate');
});
test('template without data-template-name or id should default to application', function() {
Ember.$('#qunit-fixture').html('<script type="text/x-handlebars">{{firstName}} takes {{drug}}</script>');
var id = Ember.$('#qunit-fixture .ember-view').attr('id');
ok(id && /^ember\d+$/.test(id), "has standard Ember id");
checkTemplate('application');
});
test('template with type text/x-raw-handlebars should be parsed', function() {

0 comments on commit bb149dc

Please sign in to comment.