Permalink
Browse files

Fixed each/else - fixes #389

  • Loading branch information...
1 parent 8f5abce commit eea04a5aa04ddffbe9b76fd4dbaf0c142b5e7b5a @wagenet wagenet committed Feb 11, 2012
@@ -70,7 +70,14 @@ Ember.Handlebars.registerHelper('collection', function(path, options) {
}
if (inverse && inverse !== Handlebars.VM.noop) {
- hash.emptyView = Ember.View.extend({
+ var emptyViewClass = Ember.View;
+
+ if (hash.emptyViewClass) {
+ emptyViewClass = Ember.View.detect(emptyViewClass) ?
+ hash.emptyViewClass : getPath(this, hash.emptyViewClass);
+ }
+
+ hash.emptyView = emptyViewClass.extend({
template: inverse,
tagName: itemHash.tagName
});
@@ -9,5 +9,10 @@ Ember.Handlebars.EachView = Ember.CollectionView.extend(Ember.Metamorph, {
Ember.Handlebars.registerHelper('each', function(path, options) {
options.hash.contentBinding = path;
options.hash.preserveContext = true;
+
+ // Set up emptyView as a metamorph with no tag
+ options.hash.itemTagName = '';
+ options.hash.emptyViewClass = Ember.View.extend(Ember.Metamorph);
+
return Ember.Handlebars.helpers.collection.call(this, 'Ember.Handlebars.EachView', options);
});
@@ -11,6 +11,11 @@ module("the #each helper", {
});
append(view);
+ },
+
+ teardown: function() {
+ view.destroy();
+ view = null;
}
});
@@ -63,13 +68,19 @@ test("it updates the view if an item is removed", function() {
});
assertHTML(view, "Annabelle");
- view.destroy();
});
+module("the #each helper", {
+ setup: function() {
+ people = Ember.A([{ name: "Steve Holt" }, { name: "Annabelle" }]);
+ },
+ teardown: function() {
+ if (view) { view.destroy(); }
+ view = null;
+ }
+});
test("it works inside a ul element", function() {
- view.destroy();
-
var ulView = Ember.View.create({
template: templateFor('<ul>{{#each people}}<li>{{name}}</li>{{/each}}</ul>'),
people: people
@@ -87,8 +98,6 @@ test("it works inside a ul element", function() {
});
test("it works inside a table element", function() {
- view.destroy();
-
var tableView = Ember.View.create({
template: templateFor('<table><tbody>{{#each people}}<tr><td>{{name}}</td></tr>{{/each}}</tbody></table>'),
people: people
@@ -111,3 +120,27 @@ test("it works inside a table element", function() {
equal(tableView.$('td').length, 4, "renders an additional <td> when an object is inserted at the beginning of the array");
});
+test("it supports {{else}}", function() {
+ view = Ember.View.create({
+ template: templateFor("{{#each items}}{{this}}{{else}}Nothing{{/each}}"),
+ items: Ember.A(['one', 'two'])
+ });
+
+ append(view);
+
+ assertHTML(view, "onetwo");
+
+ stop();
+
+ // We really need to make sure we get to the re-render
+ Ember.run.next(function() {
+ Ember.run(function() {
+ view.set('items', Ember.A([]));
+ });
+
+ start();
+
+ assertHTML(view, "Nothing");
+ });
+});
+

0 comments on commit eea04a5

Please sign in to comment.